subplots with plotly express 4
Asked Answered
I

1

11

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.

Intercut answered 27/7, 2019 at 22:27 Comment(0)
A
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.

Ambary answered 28/7, 2019 at 0:23 Comment(3)
will create a question immediately for the colorsIntercut
But if someone wants to add another px.line from another dataset, how can I do it?Defeasance
Link that question please?Pieper

© 2022 - 2024 — McMap. All rights reserved.