10

I have the following, using plotly express:

fig = px.line(series1, x='timestamp', y='data')
fig.show()

and it works properly.

I want to make multiple plots together, so I did:

fig = make_subplots(rows=2, cols=1)

fig.add_trace(px.line(series1, x='timestamp', y='data'), row=1, col=1)
fig.add_trace(px.line(series2, x='timestamp', y='data'), row=1, col=1)

fig.add_trace(px.line(series2, x='timestamp', y='data'), row=2, col=1)

fig.show()

but I get:

Invalid element(s) received for the 'data' property of Invalid elements include: [Figure({ 'data': [{'hoverlabel': {'namelength': 0},

although,

fig = make_subplots(rows=1, cols=2)

fig.add_trace(go.Scatter(x=series1['timestamp'], y=series1['data']), row=1, col=1)
fig.add_trace(go.Scatter(x=series2['timestamp'], y=series2['data']), row=1, col=1)
fig.add_trace(go.Scatter(x=series2['timestamp'], y=series2['data']), row=1, col=2)

fig.show()

will work; so it looks like plotly express doesn't work with subplots.

did I miss anything?

additionally, as a bonus question: I haven't found how I can specify the color for each of the traces.

Thomas
  • 10,933
  • 14
  • 65
  • 136

1 Answers1

17

Plotly Express (PX) functions like px.line() return figures, just like make_subplots does. On the other hand, add_trace() accepts trace objects and not figure objects, which is why fig.add_trace(px.line()) doesn't work.

PX can make subplots using make_subplots internally if you pass in the facet_row and/or facet_col arguments. This means that it is compatible with make_subplots in that you can call add_trace(row=r, col=c) on a figure created via px.whatever().

In the spirit of StackOverflow, I recommend splitting your "bonus question" into a separate SO question.

nicolaskruchten
  • 26,384
  • 8
  • 83
  • 101