【问题标题】:Error with Specs parameter of Plotly SubplotsPlotly Subplots 的 Specs 参数错误
【发布时间】:2023-02-14 12:05:34
【问题描述】:

我收到值错误:

make_subplots 的“specs”参数必须是维度为 (1 x 1) 的二维字典列表。 收到类型 <class 'list'> 的值:[[{'secondary_y': False}], [{'secondary_y': True}], [{'colspan': 1}, None]]

我参考了现有的帖子 plotly subplots issue with specs, value error 并按照相同的方式进行操作,但错误仍然存​​在。

下面是代码sn-p:

import talib as ta
import yfinance as yf
import pandas as pd


import plotly.io as pio
pio.renderers.default='browser'
import plotly.graph_objects as go
from plotly.subplots import make_subplots

'''
Extracting the data
'''
VIP = yf.Ticker('VIPIND.NS')
df = VIP.history(period="max")
df.reset_index(inplace = True)
df['Date'] = pd.to_datetime(df['Date'])


'''
Creating the technical indicators
'''
df['EMA_Close'] = ta.EMA(df.Close,100)
df['MA_Close']  = ta.MA(df.Close,60)

df['MACD'],df['MACDsig'],df['MACDhist']=ta.MACD(df.Close,30,60,15)

'''
###############################
Creating Plots
###############################
'''

'''
Declaring subplots
'''
fig = make_subplots(rows=2, cols=1)#, shared_xaxes=True,print_grid=True)
fig = make_subplots(specs=[[{"secondary_y": False}],[{"secondary_y": True}],[{"colspan": 1}, None]])

'''
Ploting the first row with OHLC, EMA and MA lines
'''
fig.add_trace(go.Candlestick(x=df["Date"], open=df["Open"], high=df["High"],
                low=df["Low"], close=df["Close"], name="OHLC",showlegend=True),
                row=1, col=1,secondary_y=False)

fig.add_trace(go.Scatter(x=df['Date'], y=df['EMA_Close'], showlegend=True,
                         name="EMA Close",line=dict(color="MediumPurple")
                        ), row=1, col=1,secondary_y=False)

fig.add_trace(go.Scatter(x=df['Date'], y=df['MA_Close'], showlegend=True,
                         name="MA Close",line=dict(color="Orange")
                        ), row=1, col=1,secondary_y=False)

'''
Ploting the second row with MACD & MACDSig lines and MACDHist as histogram/bar
'''
fig.add_trace(go.Bar(x=df.Date,
                     y=df['MACDhist'],showlegend=True,name="MACD Hist",marker=dict(color='black')
                    ), row=2, col=1,secondary_y=False)


fig.add_trace(go.Scatter(x=df['Date'], y=df['MACDsig'], showlegend=True,
                         name="MACD Signal",line=dict(color="MediumPurple")
                        ), row=2, col=1,secondary_y=True)

fig.add_trace(go.Scatter(x=df['Date'], y=df['MACD'], showlegend=True,
                         name="MACD",line=dict(color="red")
                        ), row=2, col=1,secondary_y=True)

'''
Upadting the layout of the plot
'''
fig.update(layout_xaxis_rangeslider_visible=False)

fig.update_layout(height=600, width=1250)

fig.update_layout(
    title='OHLC and Volume',
    yaxis_title='Prices (Rs)',
    xaxis_title='Dates')

fig.update_layout(template="plotly_white")

fig.update_layout(
    margin=dict(l=20, r=20, t=40,b=20),)

# Providing desired Fonts for the plots
fig.update_layout(
    font_family="Courier New",
    font_color="blue",
    title_font_family="Times New Roman",
    title_font_color="red",
    legend_title_font_color="green")

fig.show()

请求有关我哪里出错的指导。

问候 苏迪尔

【问题讨论】:

  • 仅当我声明次要 y 轴的规格参数时才会发生错误,因为我需要对 2ns 行中的图表使用相同的参数。

标签: subplot plotly-python


【解决方案1】:

我认为您收到错误是因为规格中的最后一个列表的长度为 2(即 [{"colspan": 1}, None]),而前两个列表中只有一个元素。 尝试这个:

fig = make_subplots(specs=[[{"secondary_y": False}],[{"secondary_y": True}],[{"colspan": 1, "secondary_y": False}]])

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 2019-03-31
    • 2021-09-10
    • 2021-06-27
    • 2019-12-06
    • 2018-03-04
    • 1970-01-01
    • 2021-09-23
    相关资源
    最近更新 更多