Plotly Express Bar chart (px.bar) labels option doesn't override Legend?

Viewed 20

I'm trying to override the default legend labels in px.bar. Am I doing this correctly?

import plotly.express as px

df = px.data.stocks(indexed=True)-1

px.bar(df, x=df.index, y=["GOOG","AAPL"],
       labels={
           "GOOG":"Google",
           "AAPL":"Apple"
       })

Output doesn't show override

1 Answers

you will need to update the figure structure with the dictionary of GOOG -> Google like this. Updated code below.

import plotly.express as px
df = px.data.stocks(indexed=True)-1
fig=px.bar(df, x=df.index, y=["GOOG","AAPL"])
newnames = {'GOOG':'Google', 'AAPL': 'Apple'}
fig.for_each_trace(lambda t: t.update(name = newnames[t.name]))

enter image description here

Related