Is there a way to use Plotly express to show multiple subplots
Asked Answered
A

3

6

I'm keen to know if there is an equivalent to:

import pandas as pd
import numpy as np

data = pd.DataFrame({'Day':range(10),
                     'Temperature': np.random.rand(10), 
                     'Wind': np.random.rand(10),
                     'Humidity': np.random.rand(10),
                     'Pressure': np.random.rand(10)})

data.set_index('Day').plot(subplots=True, layout=(2,2), figsize=(10,5))
plt.tight_layout()

enter image description here

That generates Plotly graphs as opposed to matplotlib charts.

Arrangement answered 13/1, 2021 at 5:23 Comment(0)
C
8

For a plotly express solution:
You could use pd.melt() to get all your variables in the same column:

import pandas as pd
import plotly.express as px

df = pd.DataFrame({
    'Day':range(10),
    'Temperature': np.random.rand(10), 
    'Wind': np.random.rand(10),
    'Humidity': np.random.rand(10),
    'Pressure': np.random.rand(10),})

df_melt = df.melt(
    id_vars='Day', 
    value_vars=['Temperature', 'Wind', 'Humidity', 'Pressure'])

Your dataframe now looks like this with the variable names in a column named 'variable' and the values in a column named 'value':

    Day variable    value
0   0   Temperature 0.609
1   1   Temperature 0.410
2   2   Temperature 0.194
3   3   Temperature 0.663
4   4   Temperature 0.351

Now you can use px.scatter() with argument facet_col to get the multiple plots:

fig = px.scatter(
    df_melt, 
    x='Day', 
    y='value', 
    facet_col='variable', 
    facet_col_wrap=2, 
    color='variable', 
    width=800,
)

This results in the following plot: plotly express facet_col instead of subplots

Now in your example all variables have the same range of values. But if this is not the case then you might want to make sure that every plot gets its own range on the y-axis. This can be done as follows:

fig.update_yaxes(showticklabels=True, matches=None)

More info on facet plots can be found here:
https://plotly.com/python/facet-plots/

Charest answered 13/1, 2021 at 19:30 Comment(0)
B
3
import plotly.graph_objects as go
from plotly.subplots import make_subplots

# using your sample data

fig = make_subplots(rows=2, cols=2, start_cell="bottom-left")

fig.add_trace(go.Scatter(x=data.index, y=data.Temperature, name='Temp'),
              row=1, col=1, )

fig.add_trace(go.Scatter(x=data.index, y=data.Wind, name='Wind'),
              row=1, col=2)

fig.add_trace(go.Scatter(x=data.index, y=data.Humidity, name='Humidity'),
              row=2, col=1)

fig.add_trace(go.Scatter(x=data.index, y=data.Pressure, name='Pressure'),
              row=2, col=2)

fig.show()

enter image description here

Businessman answered 13/1, 2021 at 5:53 Comment(0)
S
1

I wanted simply to plot quickly multiple distribution subplots one after another like in sns, pyplot. For loop works. Works of course also with scatter. Nice touch: even the xlables are printed.

for col in boston_df.columns.tolist():
        boston_dis = px.histogram(boston_df, 
                 x=col, color_discrete_sequence=['lavenderblush'],
                 title='Distribution',
                 histnorm='probability density', template='plotly_dark',
                 width=400, height=300)
        boston_dis.show()
Speculative answered 12/11, 2021 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.