Home > database >  Percentage ticks in plotly without multiplying with 100
Percentage ticks in plotly without multiplying with 100

Time:10-14

I think I'm going crazy. I'm trying to configure the y-axis ticks for my plotly go bar chart. It's a stacked bar chart with barnorm='percent', meaning that all bars in total get up to a 100 %. However, I can not seem to get the ticks to work correctly.

Reading the documentation from plotly gives nothing else than that they seem to think this is the default behavior and forward me to the underlying framework d3, which has a rigorous system for defining ticks. Sadly, I can not understand a thing about it.

If I pick tickformat='%' in update_layout, the tick for 100 % is 10000.000000%.

If I pick tickformat='p', the tick for 100 % is 10000%.

Both of those methods seem to multiply my value by 100. That is not what I want, since plotly has the correct number from start. I can not seem to figure out how it should be done, and I have put so much time into this now, I hope someone can help. My code is below:

animals=['giraffes', 'orangutans', 'monkeys']

fig = go.Figure(data=[
    go.Bar(name='SF Zoo', x=animals, y=[20, 14, 23]),
    go.Bar(name='LA Zoo', x=animals, y=[12, 18, 29])
])

# Change the bar mode
fig.update_layout(
        barmode='stack',
        barnorm = 'percent',
        font_family="Roboto",
        font_size=15,
        yaxis = dict(tickformat='%'),
        showlegend=True
    )

fig.show()

(Tried to upload an image of the bar chart but got some server error from SO)

CodePudding user response:

You should remove yaxis = dict(tickformat='%') and add the following:

fig.update_layout(yaxis_ticksuffix = '%')

You need to read more here to know more about how Plotly works with percentage sign.

  • Related