Bring Drag & Drop to Dash with Dashboard Engine. 💫 Learn how at our next webinar!

Plotly: Add two y-axis in the same graph

I have the following dataframe:

I want to add two lines in the same plot. Essentially landed_value_millions and wholesale_values_millions plotted over years.

The following code, which I got from Line Plot Modes gives the error ValueError: Invalid value of type 'builtins.str' received for the 'x' property of scatter. Received value: 'year' The 'x' property is an array that may be specified as a tuple, list, numpy array, or pandas Series

import plotly.graph_objects as go
salmon_value = go.Figure()

salmon_value.add_trace(go.Scatter(x='year', y='wholesale_values_millions',
                             name='whole sale value'))
salmon_value.add_trace(go.Scatter(x='year', y='landed_value_millions',
                             name='landed value'))

Anybody know how to fix this? Thank you.

go.Scatter() doesn’t accept column names for x and y, for this you’d have to use Plotly Express. If you want to use go.Scatter() you’ll have to pass in actual columns of values like x=df["year"].

1 Like