-1

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

Jeremy Yu
  • 11
  • 3

1 Answers1

0

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

Redox
  • 9,321
  • 5
  • 9
  • 26