Python pandas.io.data 保存剧情?

Python pandas.io.data save plot?

我是 Stack Overflow 的新手,也是 Python。我正在努力寻找是否有办法保存情节或人物。这可能使用这个 Pandas 包吗?我的情节显示正常,我是 Python v 2.7.11。下面的代码,谢谢

#pg 73 from Python in Finance
import numpy as np
import pandas as pd
import pandas.io.data as web



sym1 = 'AAPL'
sym2 = 'FB'

symbol1 = web.DataReader(sym1, data_source='yahoo',start='1/1/2015', end='1/28/2016')
symbol2 = web.DataReader(sym2, data_source='yahoo',start='1/1/2015', end='1/28/2016')
ratio = symbol1; 
ratio['Close'] = symbol1['Close'] / symbol2['Close'];

#symbol1['Close'].plot(grid=True, figsize=(8, 5))
#symbol2['Close'].plot(grid=True, figsize=(8, 5))

ratio['Close'].plot(grid=True, figsize=(8, 5))

ratio['42d'] = np.round(pd.rolling_mean(ratio['Close'], window=42), 2)
ratio['252d'] = np.round(pd.rolling_mean(ratio['Close'], window=252), 2)
ratio[['Close', '42d', '252d']].plot(grid=True, figsize=(8, 5))

pandas 使用 matplotlib 来绘制所以你可以使用这个

import matplotlib.pyplot as plt

plt.savefig('image.png') # save to png
plt.savefig('image.pdf') # save to pdf

在您的代码中

#pg 73 from Python in Finance
import numpy as np
import pandas as pd
import pandas.io.data as web
import matplotlib.pyplot as plt

sym1 = 'AAPL'
sym2 = 'FB'

symbol1 = web.DataReader(sym1, data_source='yahoo',start='1/1/2015', end='1/28/2016')
symbol2 = web.DataReader(sym2, data_source='yahoo',start='1/1/2015', end='1/28/2016')
ratio = symbol1; 
ratio['Close'] = symbol1['Close'] / symbol2['Close'];

#symbol1['Close'].plot(grid=True, figsize=(8, 5))
#symbol2['Close'].plot(grid=True, figsize=(8, 5))

ratio['Close'].plot(grid=True, figsize=(8, 5))

ratio['42d'] = np.round(pd.rolling_mean(ratio['Close'], window=42), 2)
ratio['252d'] = np.round(pd.rolling_mean(ratio['Close'], window=252), 2)
ratio[['Close', '42d', '252d']].plot(grid=True, figsize=(8, 5))

plt.savefig('foo.png') # save to png
plt.savefig('foo.png') # save to pdf
# plt.show() shows image

编辑: 见:

您可以使用

ax = df.plot() # your plot
fig = ax.get_figure()
fig.savefig('image.png')
import matplotlib.pyplot as plt

plt.savefig('image.png')