1

I would like to change the y-axis title of a histogram in plotly python. We could use the fig.update_layout(yaxis_title="New y-axis title") to change the title of the y-axis. But this doesn't automatically change the hover title of the y-axis. We could use the labels argument to change the title of the x-axis and hover title but this doesn't work for the y-axis title with count. Here is some reproducible code:

import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="total_bill",
                  labels={"total_bill": "Total bill"})
fig.update_layout(yaxis_title="New y-axis title")
fig.show()

Output:

enter image description here

As you can see the y-axis title is nicely changed, but the hover title isn't changed. So I was wondering if anyone knows how to fix this issue?

Quinten
  • 35,235
  • 5
  • 20
  • 53

1 Answers1

1

It seems to me you can modify @vestland's solution here.

import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="total_bill",
                  labels={"total_bill": "Total bill"})
fig.update_traces(hovertemplate ='Total bill=' + '%{x}' + '<br>'
                  'New y-axis title=' + '%{y}',
                  selector=dict(type="histogram"))
fig.show()

Or instead of update_traces just

fig.data[0]["hovertemplate"] = fig.data[0]["hovertemplate"]\
    .replace("count", "New y-axis title")
rpanai
  • 12,515
  • 2
  • 42
  • 64