通过一个小案例,学习几个matplotlib库的图像绘制
数据准本:
链接:https://pan.baidu.com/s/1KrGq3GS2gHC3i-oPofV8Ng
提取码:giao
数据如下所示:
1.文件读取及数据查看;
import pandas as pd import numpy as np import matplotlib.pyplot as plt data=pd.read_excel('/data/trd.xlsx',engine="openpyxl") #查看列名和数据 print(data.columns) print(data.iloc[:5])
dt=data.loc[data['股票代码']==600000,['交易日期','收盘价','交易量']] I1=dt['交易日期'].values>='2017-01-03' I2=dt['交易日期'].values<='2017-01-20' dta=dt.iloc[I1&I2,:] y1=dta['收盘价'] x1=range(len(y1)) plt.figure(1) plt.plot(x1,y1) plt.xlabel(u' date ',fontproperties='SimHei') plt.ylabel(u' Closing_Price ',fontproperties='SimHei') plt.title(u' Closing_Price_State ',fontproperties='SimHei') plt.savefig('1.png')
I3=dt['交易日期'].values>='2017-01-03' I4=dt['交易日期'].values<='2017-01-24' dta=dt.iloc[I3&I4,:] y2=dta['交易量'] x2=range(len(y2)) plt.figure(2) plt.bar(x2,y2) plt.xlabel(u' date ',fontproperties='SimHei') plt.ylabel(u' Volume ',fontproperties='SimHei') plt.title(u' Volume_State ',fontproperties='SimHei') plt.savefig('2.png')
D=np.zeros((11)) list1=list() for m in range(11): m=m+1 if m<10: m1='2017-0'+str(m)+'-01' m2='2017-0'+str(m)+'-31' mon='0'+str(m) else: m1='2017-'+str(m)+'-01' m2='2017-'+str(m)+'-31' mon=str(m) I1=dt['交易日期'].values>=m1 I2=dt['交易日期'].values<=m2 D[m-1]=dt.iloc[I1&I2,[2]].sum()[0] list1.append(mon) plt.figure(3) plt.pie(D,labels=list1,autopct='%1.2f%%') # 保留小数点后两位 plt.title(u'Month_Volume_State',fontproperties='SimHei') plt.savefig('3.png')
plt.figure(4) plt.figure(figsize=(14,6)) plt.subplot(1,3,1) plt.plot(x1,y1) plt.xlabel(u' date ',fontproperties='SimHei') plt.ylabel(u' Closing_Price ',fontproperties='SimHei') plt.title(u' Closing_Price_State ',fontproperties='SimHei') plt.subplot(1,3,2) plt.bar(x2,y2) plt.xlabel(u' date ',fontproperties='SimHei') plt.ylabel(u' Volume ',fontproperties='SimHei') plt.title(u' Volume_State ',fontproperties='SimHei') plt.subplot(1,3,3) plt.pie(D,labels=list1,autopct='%1.2f%%') # 保留小数点后两位 plt.title(u' Month_Volume_State ',fontproperties='SimHei') plt.savefig('4.png')