我该如何使用图表达来创建子图?

时间:2019-06-23 21:26:09

标签: python plotly

喜欢绘图的图形,但现在想用它们创建一个仪表板。找不到与此相关的任何文档。这可能吗?

3 个答案:

答案 0 :(得分:2)

我也在努力寻找对此的回应,最终创建了我自己的解决方案(请参阅此处的完整细分:How To Create Subplots Using Plotly Express

本质上'make_subplots()'接收的是轨迹而不是数字,这是Express返回的,所以在Express中创建你的图形后,你可以分解它的轨迹,然后将它们重新组合成一个子图

代码:

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)

输出:

enter image description here

答案 1 :(得分:0)

从文档中

**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.

这里也提供一些示例。

https://medium.com/@plotlygraphs/introducing-plotly-express-808df010143d

答案 2 :(得分:0)

不幸的是,目前不是。请参见以下问题以进行更新:https://github.com/plotly/plotly_express/issues/83