Python Plotly将px对象添加到子绘图对象中



所以我试图将两个图合并为一个。我用plotly.express库而不是plotly.graphs_objs绘制了这些图。现在,建议使用:fig = make_subplots(rows=3, cols=1),然后使用append_traceadd_trace但是,这对express对象不起作用,因为追加跟踪需要单个。查出如何在子情节中添加一个明确的数字?或者这根本不可能。我尝试过的一个选项是fig.data[0],但这只会添加第一行/数据条目。Rn我的代码看起来像:

double_plot = make_subplots(rows=2, cols=1, shared_xaxes=True)
histo_phases = phases_distribution(match_file_, range)
fig = px.line(match_file,
x="Minutes", y=["Communicatie", 'Gemiddelde'], color='OPPONENT')
fig.update_layout(
xaxis_title="Minuten",
yaxis_title="Communicatie per " + str(range) + "minuten",
legend_title='Tegenstander',
)

double_plot.append_trace(fig.data, row=1, col=1)
double_plot.append_trace(histo_phases.data, row=2, col=1)

提前谢谢。

  • 您的代码示例不包括数据帧和图形的创建。已模拟
  • 它很简单,只需将创建的图形中的每个轨迹添加到用make_subplots()创建的图形
for t in fig.data:
double_plot.append_trace(t, row=1, col=1)
for t in histo_phases.data:
double_plot.append_trace(t, row=2, col=1)

完整代码

from plotly.subplots import make_subplots
import plotly.express as px
import pandas as pd
import numpy as np
df = px.data.tips()
double_plot = make_subplots(rows=2, cols=1, shared_xaxes=True)
# histo_phases = phases_distribution(match_file_, range)
histo_phases = px.histogram(df, x="total_bill")
match_file = pd.DataFrame(
{
"Minutes": np.repeat(range(60), 10),
"Communicatie": np.random.uniform(1, 3, 600),
"Gemiddelde": np.random.uniform(3, 5, 600),
"OPPONENT": np.tile(list("ABCDEF"), 100),
}
)
fig = px.line(match_file, x="Minutes", y=["Communicatie", "Gemiddelde"], color="OPPONENT")
fig.update_layout(
xaxis_title="Minuten",
yaxis_title="Communicatie per " + str(range) + "minuten",
legend_title="Tegenstander",
)
for t in fig.data:
double_plot.append_trace(t, row=1, col=1)
for t in histo_phases.data:
double_plot.append_trace(t, row=2, col=1)
double_plot

最新更新