Update Legend names in .add_trace and px.line

Hi,

I have the following dataframe:

I plotted this graph using the code:

salmon_wholesale = px.line(salmon, x='year', y='wholesale_values_millions', color='species',
                          labels={'wholesale_values_millions':'Whole Sale Value (million)',
                                 'year':'Year',
                                 'species':'Species'})
salmon_wholesale.show()


And made another graph with traces:

salmon_landed = px.line(salmon, x='year', y='landed_value_millions', color='species')
salmon_landed.add_trace(salmon_wholesale.data[0])
salmon_landed.add_trace(salmon_wholesale.data[1])

salmon_landed.show()

I want to update the legend names from Farmed Salmon to Farmed Salmon - Whole Value, Farmed Salmon - Landed Value and also change the symbols or colors of each line.

Has anyone encountered a similar problem and knows how to solve this? Thank you!

Hi @aleivaar,

try this:

fig  = px.line(salmon, x='year', y=['wholesale_values_millions','landed_value_millions'], color='species',
               labels={'wholesale_values_millions':'Whole Sale Value (million)',
                       'landed_value_millions':'Landed Sale Value (million)',
                       'year':'Year',
                       'species':'Species'})
fig.show()

hope this helps :wink: , Alex-

1 Like

@Alexboiboi, Yes, this is one way to do it. Thank you!