我正在尝试绘制这个时间序列图(由几个股票代码组成的RSI图)。然而,我似乎根本不能让Plotly来绘制图表!它抛出了下面的错误:
Value of 'x' is not the name of a column in 'data_frame'. Expected one of ['Date', 'AAPL', 'GOOGL', 'IBM', 'MSFT'] but received: index To use the index, pass it in directly as **df.index**.
我猜想这个问题是因为数据帧?它有一个“多层索引”吗?
Screenshot of my DF - https://i.imgur.com/gZY7ONP.png
下面是我必须生成数据帧的代码,并从那里进行绘图。我使用了x=data.index, y=RSI.columns
,但它似乎不起作用。
#List to store my ticker codes
ticker = ['AAPL','GOOGL','IBM','MSFT']
#other variables
wrsi = ['EWMA']
mwa = 14
startdate = "2019-01-01"
enddate = "2020-10-31"
#pull data
data = web.DataReader(ticker, 'yahoo', startdate, enddate)
#RSI calculation
delta = data['Adj Close'].diff(1)
delta.dropna(inplace=True)
positive = delta.copy()
negative = delta.copy()
positive[positive < 0] = 0
negative[negative > 0] = 0
#wrsi is ewma or sma?
if wrsi == 'sma':
average_gain = positive.rolling(mwa).mean()
average_loss = abs(negative.rolling(mwa).mean())
else:
average_gain = positive.ewm(span=mwa).mean()
average_loss = abs(negative.ewm(span=mwa).mean())
relative_strength = average_gain / average_loss
RSI = 100-(100/ (1+ relative_strength))
#plot chart
fig = px.line(RSI, x=data.index, y=RSI.columns, title='Time Series with Range Slider and Selectors')
fig.update_xaxes(
rangeslider_visible=True,
rangeselector=dict(
buttons=list([
dict(count=1, label="1m", step="month", stepmode="backward"),
dict(count=6, label="6m", step="month", stepmode="backward"),
dict(count=1, label="YTD", step="year", stepmode="todate"),
dict(count=1, label="1y", step="year", stepmode="backward"),
dict(step="all")
])
)
)
fig.show()
发布于 2020-11-07 15:19:54
错误的原因是x轴上的数据与y轴上的数据不同。
fig = px.line(RSI, x=RSI.index, y=RSI.columns, title='Time Series with Range Slider and Selectors')
https://stackoverflow.com/questions/64634330
复制相似问题