Change my legend plotly python line plot area plot

Hi everyone,

I created a linear plot with plotly.py.
Below is my code :

import plotly.express as px

fig = px.area(df_CO2_regions, x="year", y="co2_billions_t", color="country",
              labels={'co2_billions_t':'CO2 billions tonnes'})

fig.update_layout(height=500, width=1300, title_text="CO2 emissions by region", title_x = 0.5)

fig.show()

and this the plot

In the legend of the colors i would like to delete the word country for each line and write it juste for the title of the legend. Also i would like to show more dates on the x axis.
Can someone know how to do that ?

Thank you

Hi @tati, Yes, you should be able to do something like:

import plotly.express as px

fig = px.area(df_CO2_regions, x="year", y="co2_billions_t", color="country",
              labels={'co2_billions_t':'CO2 billions tonnes'})

fig.update_layout(height=500, width=1300, title_text="CO2 emissions by region", title_x = 0.5)

for tr in fig.select_traces():
    tr['name'] = tr['name'].replace('country = ','')

fig.show()

To add more dates, you can put this after fig = px.area...

fig.update_xaxes(nticks=20)

Say if you want 20 dates.

Thank you very much it works :slight_smile: