[英]Is it possible to create a subplot with plotly.express?
I would like to create a subplot with 2 plot generated with the function plotly.express.line
, is it possible?我想用 function plotly.express.line
生成的 2 plot 创建一个子图,这可能吗? Given the 2 plot:鉴于 2 plot:
fig1 =px.line(df, x=df.index, y='average')
fig1.show()
fig2 = px.line(df, x=df.index, y='Volume')
fig2.show()
I would like to generate an unique plot formed by 2 subplot (in the example fig1 and fig2)我想生成一个由 2 个子图组成的唯一 plot(在示例图 1 和图 2 中)
No, not directly (as DerekO has concisely described).不,不是直接的(正如 DerekO 简洁描述的那样)。 But 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.但是由于 plotly express 可以用相当复杂的数据集做一些非常了不起的事情,我看不出为什么你不应该偶然发现你想使用 plotly express 图形的元素作为子图源的情况。 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.下面是一个示例,我在px.data.stocks()
数据集上使用px.line
构建了 plotly 表达数字。 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.然后我 go 在For Loop
中使用add_trace
和go.Scatter
提取一些感兴趣的元素以构建子图设置。 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.但话又说回来,如最初所述,plotly express 本身就是一个出色的数据处理程序。
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()
From the documentation, Plotly expressdoes not support arbitrary subplot capabilities .从文档来看,Plotly express不支持任意子图功能。 You can instead use graph objects and traces (note that go.Scatter
is equivalent):您可以改为使用图形对象和轨迹(注意go.Scatter
是等效的):
import pandas as pd
from plotly.subplots import make_subplots
import plotly.graph_objects as go
## create some random data
df = pd.DataFrame(
data={'average':[1,2,3], 'Volume':[7,3,6]},
index=['a','b','c']
)
fig = make_subplots(rows=1, cols=2)
fig.add_trace(
go.Scatter(x=df.index, y=df.average, name='average'),
row=1, col=1
)
fig.add_trace(
go.Scatter(x=df.index, y=df.Volume, name='Volume'),
row=1, col=2
)
fig.show()
There is no need to use graph_objects
module if you have just already generated px
figures for making subplots.如果您刚刚生成了用于制作子图的px
图形,则无需使用graph_objects
模块。 Here is the full code.这是完整的代码。
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)
fig.add_trace(fig1['data'][0], row=1, col=1)
fig.add_trace(fig1['data'][1], row=1, col=1)
fig.add_trace(fig2['data'][0], row=2, col=1)
fig.add_trace(fig2['data'][1], row=2, col=1)
fig.show()
If there are more than two variables in each plot, one can use for loop also to add the traces using fig.add_trace
method.如果每个 plot 中有两个以上的变量,可以使用 for 循环也可以使用fig.add_trace
方法添加跟踪。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.