简体   繁体   中英

How to create subplots with Plotly?

I had created the subplots in the seaborn. but not able to create same to same plots using plotly.

y1 = [-51.04464896560272, -50.891166658544364, -50.82896235500738,-47.93693115514636, -48.93693115514636, -50.2337317810911, -50.580211311328924, -49.63778008455918, -50.55143905012876, -47.18028476096273, -49.45950323726963, -52.759450409024325, -46.94239220924591, -49.34719404199133, -48.592737888881025, -50.39057938285388,-50.33666292445022,-49.689436282011854,-49.86872483079463, -49.47463251418676,-51.542982170986264, -50.650742675143576, -46.498494705109124, -69.232249295994,-65.83208484345344,-56.624359510655715, -64.65663405050996, -63.385051075806444, -61.4890501679952, -64.5215014815885]
y2 = [-49.22994660618851, -49.93693115514636,-47.48816812312157, -47.642008385846054, -49.24556971152835, -49.003594660143946, -49.92614593515235, -48.79589239042106, -49.90953636053393, -49.84547212502676, -48.83995760926475, -50.399185030856515, -47.98870212151289, -49.68206139796429, -48.82322978146867, -49.76209077328067, -48.3489893943433, -49.7807786904155, -51.025277583740525, -46.11152498485985, -17.51108674058229, -6.141613959880809, -107.74424733083552, 24.898229249313353, -27.273007692798586, -26.553484680185544, -38.6166124915005, -29.34043563361383, 31.648897538128374, 52.881233935942774]

t = [50.0,51.0,52.0,53.0,54.0,55.0,56.0,57.0,58.0,59.0,59.0,59.0,60.0,61.0, 59.0,63.0,64.0, 65.0, 66.0,67.0, 68.0,69.0, 70.0,71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0]

dd= ['0','50','79','80','88','95','0','50','79','80','88','95','0','50','79','80','88','95','88','95','79','80','50','79','95','0','50','88','88','95']
df = pd.DataFrame({'y1':y1,'y2':y2,'t':t,'dd':dd})
fig, axes = plt.subplots(2,1, sharex=True, figsize=(17,10))
sns.lineplot(ax=axes[0],hue='dd',x='t', y=y1, data=df, legend='full', ci=None)
sns.lineplot(ax=axes[1],hue='dd',x='t', y=y2, data=df, legend='full', ci=None)
plt.show();

在此处输入图像描述

I want same graph in plotly.

I tried

import plotly.graph_objects as go

fig = px.line(rmc, y='y1', x='t',color=df['dd'],title='plot')
fig = px.line(rmc, y='y2', x='t', title='plot', color=df['dd'])
fig.show();

thank you in advance!!

Based on the example I commented on, I created a code with an updated data frame. I have specified 6 colors from the plotly default color set so that each graph has a common line color. The second is hidden because it is automatically assigned a legend. I have also added subtitle and main title settings.

from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.express as px

colors = px.colors.qualitative.Plotly[:6]
print(colors)

fig = make_subplots(
    rows=2, cols=1,
    subplot_titles=("Ttitle 1", "Title 2"))

for d,c in zip(df['dd'].unique(), colors):
    dff = df.query('dd == @d')
    fig.add_trace(go.Scatter(x=dff['t'], y=dff['y1'], mode='lines', line_color=c, name=d), row=1, col=1)
    fig.add_trace(go.Scatter(x=dff['t'], y=dff['y2'], mode='lines', line_color=c, name=d, showlegend=False), row=2, col=1)

fig.update_layout(title_text="Multiple Subplots with Titles")
fig.show()

在此处输入图像描述

Plotly express handles this beautifully with px.line if you include the facet_row argument to produce a trellis or facet plot. Both of which are practically just other terms for a subplot. If you're willing to perform a slight manipulation of your dataset so that y1 and y2 appear in the same column like this:

pd.melt(df, id_vars= ['t', 'dd'], value_vars = ['y1', 'y2'])

...then all you have to do in order to produce the plot below is:

px.line(df2, y='value', x='t', title='plot', color = 'dd', facet_row = 'variable')

Plot

在此处输入图像描述

Complete snippet

import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd

y1 = [-51.04464896560272, -50.891166658544364, -50.82896235500738,-47.93693115514636, -48.93693115514636, -50.2337317810911, -50.580211311328924, -49.63778008455918, -50.55143905012876, -47.18028476096273, -49.45950323726963, -52.759450409024325, -46.94239220924591, -49.34719404199133, -48.592737888881025, -50.39057938285388,-50.33666292445022,-49.689436282011854,-49.86872483079463, -49.47463251418676,-51.542982170986264, -50.650742675143576, -46.498494705109124, -69.232249295994,-65.83208484345344,-56.624359510655715, -64.65663405050996, -63.385051075806444, -61.4890501679952, -64.5215014815885]
y2 = [-49.22994660618851, -49.93693115514636,-47.48816812312157, -47.642008385846054, -49.24556971152835, -49.003594660143946, -49.92614593515235, -48.79589239042106, -49.90953636053393, -49.84547212502676, -48.83995760926475, -50.399185030856515, -47.98870212151289, -49.68206139796429, -48.82322978146867, -49.76209077328067, -48.3489893943433, -49.7807786904155, -51.025277583740525, -46.11152498485985, -17.51108674058229, -6.141613959880809, -107.74424733083552, 24.898229249313353, -27.273007692798586, -26.553484680185544, -38.6166124915005, -29.34043563361383, 31.648897538128374, 52.881233935942774]

t = [50.0,51.0,52.0,53.0,54.0,55.0,56.0,57.0,58.0,59.0,59.0,59.0,60.0,61.0, 59.0,63.0,64.0, 65.0, 66.0,67.0, 68.0,69.0, 70.0,71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0]

dd= ['0','50','79','80','88','95','0','50','79','80','88','95','0','50','79','80','88','95','88','95','79','80','50','79','95','0','50','88','88','95']
df = pd.DataFrame({'y1':y1,'y2':y2,'t':t,'dd':dd})

df2 = pd.melt(df, id_vars= ['t', 'dd'], value_vars = ['y1', 'y2'])

fig = px.line(df2, y='value', x='t', title='Trellis / Facet plot', color = 'dd', facet_row = 'variable')

fig.show()

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