Aviv Fried
Aviv Fried

Reputation: 11

Reducing decimal places in Plotly histnorm

I am trying to reduce the decimal places in the text above the histogram bins to 2 places. Here is the graph:

enter image description here

And, here is the code:

fig = px.histogram(df10, x="S_Hour", histnorm='percent', text_auto=True,
                   labels=dict(S_Hour='Hours of the Day'))
fig.update_layout(yaxis_title="Percents (%)")
fig.update_layout(xaxis = dict(tickmode = 'linear', tick0 = 0, dtick = 1))
fig.update_traces(textfont_size=12, textangle=0, textposition="outside",cliponaxis=False)
fig.show()

Thanks.

Upvotes: 1

Views: 3723

Answers (1)

Redox
Redox

Reputation: 10017

To set the number of decimals in the display, you will need to add texttemplate='%{y:.2f}' inside your update_trace. This will tell plotly that you want the display of y values in the format .2f meaning 2 decimal places after the decimal point. As the data was not available, I have used the plotly tips data to display the same as an example.

import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="total_bill", y="tip", histfunc="avg", nbins=8, text_auto=True)
fig.update_traces(textfont_size=12, textangle=0, textposition="outside", cliponaxis=False, texttemplate='%{y:.2f}')
fig.show()

Plot

enter image description here

Upvotes: 4

Related Questions