Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i create subplots with plotly express?

Tags:

python

plotly

been loving the plotly express graphs but want to create a dashboard with them now. Did not find any documentation for this. Is this possible?

like image 968
Animesh Dwivedi Avatar asked Jun 23 '19 21:06

Animesh Dwivedi


People also ask

Can I make subplots with Plotly Express?

Plotly Express does not support arbitrary subplot capabilities, instead it supports faceting by a given data dimension, and it also supports marginal charts to display distribution information.

What is the difference between Plotly and Plotly Express?

Comparing Graph Objects and Plotly Express More complex figures such as sunbursts, parallel coordinates, facet plots or animations require many more lines of figure-specific graph objects code, whereas switching from one representation to another with Plotly Express usually involves changing just a few characters.

How to create figures with mixed subplots in Plotly Express?

Note: At this time, Plotly Express does not support creating figures with arbitrary mixed subplots i.e. figures with subplots of different types. Plotly Express only supports facet plots and marginal distribution subplots. To make a figure with mixed subplots, use the make_subplots () function in conjunction with graph objects as documented below.

What is a subplot in Plotly?

Subplots and Plotly Express Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures.

What is the use of Plotly Express?

Plotly Express is the easy-to-use, high-level interface to Plotly, which operates on a variety of types of data and produces easy-to-style figures. Note: At this time, Plotly Express does not support creating figures with arbitrary mixed subplots i.e. figures with subplots of different types.

How do I use make_subplots ()?

Essentially make_subplots () takes in plot traces to make the subplots instead of figure objects like that which Express returns. So what you can do is, after creating your figures in Express, is break apart the Express figure objects into their traces and then re-assemble their traces into subplots.


4 Answers

I was struggling to find a response on this as well so I ended up having to create my own solution (see my full breakdown here: How To Create Subplots Using Plotly Express)

Essentially make_subplots() takes in plot traces to make the subplots instead of figure objects like that which Express returns. So what you can do is, after creating your figures in Express, is break apart the Express figure objects into their traces and then re-assemble their traces into subplots.

Code:

import dash_core_components as dcc
import plotly.express as px
import plotly.subplots as sp


# Create figures in Express
figure1 = px.line(my_df)
figure2 = px.bar(my_df)

# For as many traces that exist per Express figure, get the traces from each plot and store them in an array.
# This is essentially breaking down the Express fig into it's traces
figure1_traces = []
figure2_traces = []
for trace in range(len(figure1["data"])):
    figure1_traces.append(figure1["data"][trace])
for trace in range(len(figure2["data"])):
    figure2_traces.append(figure2["data"][trace])

#Create a 1x2 subplot
this_figure = sp.make_subplots(rows=1, cols=2) 

# Get the Express fig broken down as traces and add the traces to the proper plot within in the subplot
for traces in figure1_traces:
    this_figure.append_trace(traces, row=1, col=1)
for traces in figure2_traces:
    this_figure.append_trace(traces, row=1, col=2)

#the subplot as shown in the above image
final_graph = dcc.Graph(figure=this_figure)

Output:

enter image description here

like image 104
mmarion Avatar answered Oct 23 '22 09:10

mmarion


Working off @mmarion's solution:

import plotly.express as px
from plotly.offline import plot
from plotly.subplots import make_subplots

figures = [
            px.line(df1),
            px.line(df2)
    ]

fig = make_subplots(rows=len(figures), cols=1) 

for i, figure in enumerate(figures):
    for trace in range(len(figure["data"])):
        fig.append_trace(figure["data"][trace], row=i+1, col=1)
        
plot(fig)

This is easily extended into the column dimension.

like image 37
XiB Avatar answered Oct 23 '22 07:10

XiB


From the docs:

**facet_row**
(string: name of column in data_frame) Values from this column are used to assign marks to facetted subplots in the vertical direction.
**facet_col**
(string: name of column in data_frame) Values from this column are used to assign marks to facetted subplots in the horizontal direction.

Get here some examples too.

https://medium.com/@plotlygraphs/introducing-plotly-express-808df010143d

like image 31
PythonNoob Avatar answered Oct 23 '22 08:10

PythonNoob


Unfortunately, it is not at the moment. See the following issue to get updated: https://github.com/plotly/plotly_express/issues/83

like image 30
hrng Avatar answered Oct 23 '22 09:10

hrng