plotly subplots 问题与规格,值错误

plotly subplots issue with specs, value error

我正在尝试绘制 3 个带有次要 y 轴的子图和 plotly 子图。 但是无论我尝试什么,仍然会出现此类错误。

值错误: make_subplots 的 'specs' 参数必须是维度为 (3 x 1) 的二维字典列表。 收到类型 的值:[[{'secondary_y': True}, {'secondary_y': True}, {'secondary_y': True}]]

感谢您的帮助

import pandas as pd
from plotly.subplots import make_subplots

data1 = {'number': [1,2,3,4], '1value': [20, 21, 19, 18],'2value': [30, 40, 65, 80],'3value': [400, 500, 600, 700]}  
df1 = pd.DataFrame(data1)  

data2 = {'number': [1,2,3,4], '1value': [80, 120, 100, 200],'2value': [300, 60, 80, 150],'3value': [700, 800, 900, 1000]}  
df2 = pd.DataFrame(data2)

data3 = {'number': [1,2,3,4], '1value': [150, 200, 100, 170],'2value': [350, 40, 500, 200],'3value': [100, 200, 300, 400]}  
df3 = pd.DataFrame(data3)


fig = make_subplots (rows=3,cols=1,vertical_spacing=0.2,
                         specs=[[{"secondary_y": True}, {"secondary_y": True}, {"secondary_y": True}]])

    
fig.add_trace(go.Scatter(x=df1["number"], y = round(df1['1value'],1), mode = 'lines'), row=1, col=1,secondary_y=False)
fig.add_trace(go.Scatter(x=df1["number"], y = round(df1['2value'],1), mode = 'lines'),row=1, col=1,secondary_y=False)
fig.add_trace(go.Scatter(x=df1["number"], y = round(df1['3value'],1), mode = 'lines'),row=1, col=1,secondary_y=True)

fig.add_trace(go.Scatter(x=df2["number"], y = df2['1value'], mode = 'lines'), row=2, col=1,secondary_y=False)
fig.add_trace(go.Scatter(x=df2["number"], y = df2['2value'], mode = 'lines'), row=2, col=1,secondary_y=False)
fig.add_trace(go.Scatter(x=df2["number"], y = df2['3value'], mode = 'lines'), row=2, col=1,secondary_y=False)
    
fig.add_trace(go.Scatter(x=d3["number"], y = df3['1value'], mode = 'lines'), row=3, col=1,secondary_y=False)
fig.add_trace(go.Scatter(x=d3["number"], y = df3['2value'], mode = 'lines'), row=3, col=1,secondary_y=False)
fig.add_trace(go.Scatter(x=d3["number"], y = df3['3value'], mode = 'lines'), row=3, col=1,secondary_y=False)

fig.show()

和错误信息完全一样。需要 2D 3x1 规格数组。您定义了一个长度为 3 的一维数组。

fig = make_subplots (rows=3,cols=1,vertical_spacing=0.2,
                         specs=[[{"secondary_y": True}], [{"secondary_y": True}], [{"secondary_y": True}]])

完整代码

import pandas as pd
from plotly.subplots import make_subplots
import plotly.graph_objects as go

data1 = {'number': [1,2,3,4], '1value': [20, 21, 19, 18],'2value': [30, 40, 65, 80],'3value': [400, 500, 600, 700]}  
df1 = pd.DataFrame(data1)  

data2 = {'number': [1,2,3,4], '1value': [80, 120, 100, 200],'2value': [300, 60, 80, 150],'3value': [700, 800, 900, 1000]}  
df2 = pd.DataFrame(data2)

data3 = {'number': [1,2,3,4], '1value': [150, 200, 100, 170],'2value': [350, 40, 500, 200],'3value': [100, 200, 300, 400]}  
df3 = pd.DataFrame(data3)


fig = make_subplots (rows=3,cols=1,vertical_spacing=0.2,
                         specs=[[{"secondary_y": True}], [{"secondary_y": True}], [{"secondary_y": True}]])

    
fig.add_trace(go.Scatter(x=df1["number"], y = round(df1['1value'],1), mode = 'lines'), row=1, col=1,secondary_y=False)
fig.add_trace(go.Scatter(x=df1["number"], y = round(df1['2value'],1), mode = 'lines'),row=1, col=1,secondary_y=False)
fig.add_trace(go.Scatter(x=df1["number"], y = round(df1['3value'],1), mode = 'lines'),row=1, col=1,secondary_y=True)

fig.add_trace(go.Scatter(x=df2["number"], y = df2['1value'], mode = 'lines'), row=2, col=1,secondary_y=False)
fig.add_trace(go.Scatter(x=df2["number"], y = df2['2value'], mode = 'lines'), row=2, col=1,secondary_y=False)
fig.add_trace(go.Scatter(x=df2["number"], y = df2['3value'], mode = 'lines'), row=2, col=1,secondary_y=False)
    
fig.add_trace(go.Scatter(x=df3["number"], y = df3['1value'], mode = 'lines'), row=3, col=1,secondary_y=False)
fig.add_trace(go.Scatter(x=df3["number"], y = df3['2value'], mode = 'lines'), row=3, col=1,secondary_y=False)
fig.add_trace(go.Scatter(x=df3["number"], y = df3['3value'], mode = 'lines'), row=3, col=1,secondary_y=False)