我如何使用 plotly express 创建子图?

新手上路,请多包涵

一直喜欢情节表达的图表,但现在想用它们创建一个仪表板。没有找到这方面的任何文档。这可能吗?

原文由 Animesh Dwivedi 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 536
1 个回答

我也在努力寻找对此的回应,所以我最终不得不创建自己的解决方案(请在此处查看我的完整细分: 如何使用 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)

输出:

在此处输入图像描述

原文由 mmarion 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题