如何解决无法作图-说'x'的值不是列名
我正在尝试绘制此时间序列图(一些股票报价器的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','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",dict(count=1,label="YTD",step="year",stepmode="todate"),label="1y",dict(step="all")
])
)
)
fig.show()
解决方法
错误原因是x轴上的数据与y轴上的数据不同。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label style="font-weight: bold">Choose the literary genres</label>
<select name="genre" id="genre" multiple>
<option value="all">All genres</option>
<option value="classic">Classic</option>
<option value="romance">Romance</option>
<option value="sciencefiction">Science fiction</option>
</select>
<label style="font-weight: bold">Choose the books</label>
<select disabled="disabled" class="subcat" id="book" name="book" multiple>
<option value>All books</option>
<!-- Home Ware -->
<optgroup data-rel="classic">
<option value="classic_book_1">classic book 1</option>
<option value="classic_book_2">classic book 2</option>
<option value="classic_book_3">classic book 3</option>
</optgroup>
<!-- Education -->
<optgroup data-rel="romance">
<option value="romance_book_1">romance book 1</option>
<option value="romance_book_2">romance book 2</option>
<option value="romance_book_3">romance book 3</option>
</optgroup>
<!-- Books -->
<optgroup data-rel="sciencefiction">
<option value="sciencefiction_book_1">science fiction book 1</option>
<option value="sciencefiction_book_2">science fiction book 2</option>
<option value="sciencefiction_book_3">science fiction book 3</option>
</optgroup>
</select>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。