Python 将 px 对象添加到子图对象中
Python Plotly adding px objects to a subplot object
所以我想把两个地块合二为一。我用 plotly.express 库而不是 plotly.graphs_objs 绘制了这些图。
现在,plotly 建议使用:fig = make_subplots(rows=3, cols=1)
然后 append_trace
或 add_trace
但是,这不适用于 express 对象,因为追加跟踪需要一个。痕迹。如何向子图中添加快速图形?或者这根本不可能。我尝试过的一个选项是 fig.data[0]
但这只会添加第一个 line/data 条目。 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)
提前致谢。
- 您的代码示例不包括创建数据框和图形。模拟过
- 就像将使用 plotly express 创建的图形的每条轨迹添加到使用
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
所以我想把两个地块合二为一。我用 plotly.express 库而不是 plotly.graphs_objs 绘制了这些图。
现在,plotly 建议使用:fig = make_subplots(rows=3, cols=1)
然后 append_trace
或 add_trace
但是,这不适用于 express 对象,因为追加跟踪需要一个。痕迹。如何向子图中添加快速图形?或者这根本不可能。我尝试过的一个选项是 fig.data[0]
但这只会添加第一个 line/data 条目。 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)
提前致谢。
- 您的代码示例不包括创建数据框和图形。模拟过
- 就像将使用 plotly express 创建的图形的每条轨迹添加到使用
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