简体   繁体   中英

subplots with plotly express 4

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM