Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 985
Thomas Avatar asked Jul 27 '19 22:07

Thomas


People also ask

Can you use Plotly Express in subplots?

Creating subplots in plotly is not supported using plotly-express . Instead, we have to manually build our figures using plotly graph objects Plotly Express usually hides this step from you.

How do I add a table to a subplot plotly?

In Plotly there is no native way to insert a Plotly Table into a Subplot. To do this, create your own Layout object and defining multiple xaxis and yaxis to split up the chart area into different domains.

What is the difference between plotly and Plotly Express?

Using Plotly, it is easy to create a Dashboard. The difference from the Plotly Express is that you will have to use plotly. graph_objects as go instead of plotly express. The gist below in my GitHub has the entire code that you can use as a template to create your own visualizations.


1 Answers

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.

like image 99
nicolaskruchten Avatar answered Oct 17 '22 07:10

nicolaskruchten