我正在使用以下代码在 Plotly 中绘制两个图形:
d = {'1': pd.DataFrame({'x': [1,2,3], 'y': [2,4,6]}), '2': pd.DataFrame({'x': [2,4,6], 'y': [4,6,8]})}
def p1(df, n):
x = df.x.tolist()
y = df.y.tolist()
y_upper = (2*df.y).tolist()
y_lower = (0.5*df.y).tolist()
fig = go.Figure([
go.Scatter(
x=x,
y=y,
mode='lines',
name=n,
showlegend=True
),
go.Scatter(
x=x+x[::-1],
y=y_upper+y_lower[::-1],
fill='toself',
line=dict(color='rgba(255,255,255,0)'),
hoverinfo='skip',
showlegend=False
)
])
return fig
def p2():
fig_data = tuple(p1(df, n).data for n, df in d.items())
fig = go.Figure(sum(zip(*fig_data), ()))
fig.update_layout(xaxis=dict(range=[0, 5],
showgrid=True),
yaxis=dict(showgrid=True))
return fig
def p3():
fig = go.Figure()
for (n, df) in d.items():
y = df.iloc[1]
fig.add_trace(go.Bar(
x=['x', 'y', 'z'],
y=y,
name=n,
text=y,
textposition='outside'
))
fig.update_layout(bargroupgap=0.1, barmode='group')
return fig
我想在子图中绘制这两个数字,我按照 Plotly 官方网站上的 example 进行操作:
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=1, cols=2)
fig.add_trace(
p3().data[0],
row=1, col=1
)
fig.add_trace(
p2().data[0],
row=1, col=2
)
fig.update_layout(height=600, width=800, title_text="Side By Side Subplots")
fig.show()
这不起作用,因为我的人物不仅仅是
data[0]
元素。有没有办法将两个图的所有 data
元素包含在子图中?