简体   繁体   English

使用 for 循环绘制多个移动平均线

[英]plotting multiple moving averages using for loop

I am trying to plot multiple moving averages using a for loop but when i put in this code i get an error.我正在尝试使用 for 循环 plot 多个移动平均线,但是当我输入此代码时出现错误。

ma_day = [10,20,50]
for ma in ma_day:
column_name = "MA in %s days" %(str(ma))

AAPL[column_name]= AAPL['Adj Close'].rolling(window=ma).mean()
AAPL[['Adj Close', 'MA for 10 days', 'MA for 20 days', 'MA for 50 days']].plot(subplots=False, kind='line', 
                                                                       figsize=(10,4))

the error i get says MA for 10 days , MA for 20 days , MA for 50days not in index.我得到的错误说MA for 10 daysMA for 20 daysMA for 50days天不在索引中。 what should be the correct code please?请问正确的代码应该是什么?

Well, I would match the column names in your attempted plot to the actual column names.好吧,我会将您尝试的 plot 中的列名与实际的列名匹配。

In the line above, you are naming the columns with " in " and in your plotting you are using " for " in the name.在上面的行中,您使用“ in ”命名列,并且在绘图中使用名称中的“ for ”。

Yer pronouns gotta match: :)你的代词必须匹配::)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM