#数据源: http://pan.baidu.com/s/1hs5Wn0w
#要求: Matlibplot Simple Application
#1. Chart the price trend of the day-line level from June 2017 to August based on the data.
#2. Overlay MA5, MA10, MA20 into the diagram
ImportPandas as PDImportNumPy as NPImportMatplotlib.pyplot as PltImportMatplotlib.finance as MPFImportdatetime fromMatplotlib.pylabImportdate2numplt.rcparams['Font.sans-serif'] = ['Simhei']plt.rcparams['Axes.unicode_minus'] =False#read data and remove excess dataData=pd.read_csv ('000001.sz.csv', encoding='GBK', index_col=0). Iloc[:-2,:4]#To adjust an index to a datetime formatdata.index=pd.to_datetime (Data.index)#aggregating minute data into daily datadata_open=data.loc[:,'Open Price (yuan)'].resample ('D'). First (). Dropna () Data_high=data.loc[:,'high Price (yuan)'].resample ('D'). Max (). Dropna () Data_low=data.loc[:,'lowest price (yuan)'].resample ('D'). Min (). Dropna () Data_close=data.loc[:,'Close Price (yuan)'].resample ('D'). Last (). Dropna ()#Merge Open, close, highest, lowest data, note data order, consistent with Candlestick_ochlNew_data=pd.concat ([Data_open,data_close,data_high,data_low],axis=1)#new_data=new_data.ix[' 2017-06 ': ' 2017-08 '];p rint (new_data)#to adjust a date index to a columnNew_data=New_data.reset_index ()#Convert a date to num formatlst=[] forIinchRange (len (new_data)): lst.append (int (date2num (new_data.iloc[i,0 ))) new_data['Date']=lstquotes=Np.array (new_data) Fig,ax=plt.subplots (figsize= (8,5)) Mpf.candlestick_ochl (Ax,quotes,width=1,colorup='g', colordown='R')#draw the figure of the moving averages 5th, 10th and 20th respectivelynew_data.index=new_data['Date']pd.rolling_mean (new_data['Close Price (yuan)'],5). Plot () Pd.rolling_mean (new_data['Close Price (yuan)'],10). Plot () Pd.rolling_mean (new_data['Close Price (yuan)'],20). Plot ()#set X axis to date, adjust X-axis date rangeax.xaxis_date () Ax.set_xlim (Datetime.datetime (2017,6,1), Datetime.datetime (2017,8,31) ) plt.show ()
Python Draw Candlestick Chart