Is it possible to create a subplot with Plotly Express?

Yes, you can build subplots using plotly express. Either

1. directly through the arguments facet_row and facet_colums (in which case we often talk about facet plots, but they’re the same thing), or

2. indirectly through “stealing” elements from figures built with plotly express and using them in a standard make_subplots() setup with fig.add_traces()


Method 1: Facet and Trellis Plots in Python

Although plotly.express supports data of both wide and long format, I often prefer building facet plots from the latter. If you have a dataset such as this:

    Date variable  value
0  2019-11-04  average      4
1  2019-11-04  average      2
.
.
8  2019-12-30   volume      5
9  2019-12-30   volume      2

then you can build your subplots through:

fig = px.line(df, x='Date', y = 'value', facet_row = 'variable')

Plot 1:

enter image description here

By default, px.line() will apply the same color to both lines, but you can easily handle that through:

fig.update_traces(line_color)

This complete snippet shows you how:

import plotly.graph_objects as go
import plotly.express as px
import pandas as pd

df = pd.DataFrame({'Date': ['2019-11-04', '2019-11-04', '2019-11-18', '2019-11-18', '2019-12-16', '2019-12-16', '2019-12-30', '2019-12-30'],
                  'variable':['average', 'volume', 'average', 'volume', 'average','volume','average','volume'],
                  'value': [4,2,6,5,6,7,5,2]})

fig = px.line(df, x='Date', y = 'value', facet_row = 'variable')

fig.update_traces(line_color="red", row = 2)

fig.show()

Method 2: make_subplots

Since plotly express can do some pretty amazing stuff with fairly complicated datasets, I see no reason why you should not stumple upon cases where you would like to use elements of a plotly express figure as a source for a subplot. And that is very possible.

Below is an example where I’ve built to plotly express figures using px.line on the px.data.stocks() dataset. Then I go on to extract some elements of interest using add_trace and go.Scatter in a For Loop to build a subplot setup. You could certainly argue that you could just as easily do this directly on the data source. But then again, as initially stated, plotly express can be an excellent data handler in itself.

Plot 2: Subplots using plotly express figures as source:

enter image description here

Complete code:

import plotly.graph_objects as go
import plotly.express as px
import pandas as pd

from plotly.subplots import make_subplots

df = px.data.stocks().set_index('date')

fig1 = px.line(df[['GOOG', 'AAPL']])
fig2 = px.line(df[['AMZN', 'MSFT']])

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

for d in fig1.data:
    fig.add_trace((go.Scatter(x=d['x'], y=d['y'], name = d['name'])), row=1, col=1)
        
for d in fig2.data:
    fig.add_trace((go.Scatter(x=d['x'], y=d['y'],  name = d['name'])), row=2, col=1)
    
fig.show()

Leave a Comment