mma*_*ion 33
我也很难找到对此的回应,所以我最终不得不创建自己的解决方案(请参阅此处的完整细分:如何使用 Plotly Express 创建子图)
本质上make_subplots()是采用绘图轨迹来制作子图,而不是像 Express 返回的图形对象。因此,您可以做的是,在 Express 中创建图形后,将 Express 图形对象分解为其轨迹,然后将其轨迹重新组装成子图。
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)
Run Code Online (Sandbox Code Playgroud)
XiB*_*XiB 20
解决@mmarion的解决方案:
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)
Run Code Online (Sandbox Code Playgroud)
这很容易扩展到列维度。
来自文档:
**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.
Run Code Online (Sandbox Code Playgroud)
这里也有一些例子。
https://medium.com/@plotlygraphs/introducing-plotly-express-808df010143d