Python pandas.io.data save plot? -


i new stack overflow , python well. struggling find if there way save plot or figure. possible using pandas package? plot displays ok , python v 2.7.11. code below ,

#pg 73 python in finance import numpy np import pandas pd import pandas.io.data 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 use matplotlib draw can use

import matplotlib.pyplot plt  plt.savefig('image.png') # save png plt.savefig('image.pdf') # save pdf 

in code

#pg 73 python in finance import numpy np import pandas pd import pandas.io.data web import matplotlib.pyplot 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 png plt.savefig('foo.png') # save pdf # plt.show() shows image 

edit: see: https://stackoverflow.com/a/25588487/1832058

you can use

ax = df.plot() # plot fig = ax.get_figure() fig.savefig('image.png') 

Comments