Plotly express legend


How i can remove the “country=” not to be part of every legend?

import plotly.express as px

fig = px.scatter(df, x="rank", y="value", color='country', animation_frame="year", 
                  size='value', hover_data=['name'], width=1100, height=600)
fig.show()
1 Like

Hi @rpeter,
since the legend item of a trace is given by its name attribute, you can iterate through the traces to modify the names as below (as a rule of thumb, it helps to do print(fig) to see the structure of the figure and understand how to modify it).

import plotly.express as px
df = px.data.gapminder().query("year == 2007").query("continent == 'Europe'")
fig = px.scatter(df, x='gdpPercap', y='lifeExp', color='country')
for trace in fig.data:
    trace.name = trace.name.split('=')[1]
fig.show()

That said, legend titles is something we want to add in plotly.py and when it’s implemented the name of the column will be the title, so by default you will not have the column name repeated in legend items. Please stay tuned for future developments :-).

1 Like

Now I’m scratching my head… and thinking… "why I didn’t think about that before posting… :smiley: "

Thank you, i appreciate your time.