有没有办法使图2成为图1的子图


from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig1 = px.area(CPI_and_Interest, x=CPI_and_Interest.index, y=Chi_so)

fig2 = px.line(CPI_and_Interest, x=CPI_and_Interest.index, y=[‘Overnight’])

fig = make_subplots(rows=2, cols=1,
shared_xaxes=True,
vertical_spacing=0.02)

fig.add_trace(fig1, row=1, col=1)
fig.add_trace(fig2, row=2, col=1)

fig.show()

我一直在尝试用上面的两个情节(图1和图2(创建一个子情节。然而,它不断向我显示以下错误:

All remaining properties are passed to the constructor of the specified trace type
(e.g. [{'type': 'scatter', ...}, {'type': 'bar, ...}])

有什么办法解决这个问题吗?

您使用的是带有add_trace的plotly.express。相反,请使用plotly.graph_objects。像这样:

from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=1, cols=2)
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[4, 5, 6]),
row=1, col=1)
fig.add_trace(
go.Scatter(x=[20, 30, 40], y=[50, 60, 70]),
row=1, col=2)

fig.show()

如需进一步澄清,请阅读以下文件:https://plotly.com/python/subplots/

最新更新