如何使用Plotly Express创建子地块

2024-03-28 14:29:28 发布

您现在位置:Python中文网/ 问答频道 /正文

如果你和我一样,你喜欢Plotly Express,但是当你遇到Express返回的数字不能使用“make_subplot()”的问题时,你很沮丧,因为make_subplot接受的是痕迹而不是数字。在这篇文章中,我想分享我自己的解决方案,我如何仅使用Plotly Express(和Plotly.subplot)创建包含两种不同类型图形的子图(如下所示)

enter image description here


Tags: 图形类型make数字plotly解决方案express痕迹
1条回答
网友
1楼 · 发布于 2024-03-28 14:29:28

解决方案:

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)

由于我正在处理的项目数据的敏感性,我无法共享程序输出的实际图像,但它与上图中的图像完全相同。据我测试,这应该适用于任何明确的数字

我希望这对一些人有用。大家好

相关问题 更多 >