我正在尝试自动绘制股票价格及其移动平均线。我有一个股票清单,我想用股票价格,50天移动平均线和200天移动平均线创建一个图表。我想为我列表中的x个股票执行此操作。当我运行它时,为什么他们在同一个图表上而不是单独地绘制在彼此之上?
import pandas as pd
from pandas import DataFrame
from matplotlib import pyplot as plt
import pandas.io.data as web
import datetime as dt
stocks = ['AAPL', 'DVN', 'XOM']
start = '2010-01-01'
end = dt.datetime.today()
def plotStock(stock):
df = web.DataReader(stock, 'yahoo', start, end)['Adj Close']
df.plot()
pd.rolling_mean(df, 50).plot(style='k')
pd.rolling_mean(df, 200).plot(style='--')
plt.title(stock, fontsize=10)
plt.savefig(stock + '.png', bbox_inches='tight')
for stock in stocks:
plotStock(stock)