Python數據分析—matplotlib可視化
- 2020 年 3 月 10 日
- 筆記
偶然看到網上國家統計數據,利用Python數據分析自己做了幾種圖表練習。主要採用Pandas來做數據統計,matplotlib來做圖表可視化。

下面圖表數據來源於網絡。
堆疊柱狀圖


代碼如下:
import numpy as np import matplotlib.pyplot as plt import pandas as pd from matplotlib.font_manager import FontProperties plt.rcParams['font.family']='sans-serif' plt.rcParams['font.sans-serif']='SimHei' df=pd.read_excel('d:/網絡收集數據.xlsx') df2=pd.read_excel('d:/網絡收集數據.xlsx',1) regionTypes=["省轄市","市轄區"] width = 0.35 # the width of the bars: can also be len(x) sequence def getPlot(regionType): fig, ax = plt.subplots(figsize=(12,6)) g=df2.groupby(df2['城市名']).sum() labels=g.index y1=g['2018年二氧化硫_%s'%regionType].apply(round) y2=g['2018年氮氧化物_%s'%regionType].apply(round) y3=g['2018年煙塵_%s'%regionType].apply(round) y21=g['2019年二氧化硫_%s'%regionType].apply(round) y22=g['2019年氮氧化物_%s'%regionType].apply(round) y23=g['2019年煙塵_%s'%regionType].apply(round) x = np.arange(len(labels)) # the label locations rects1=ax.bar(x-width/2.0, y1, width, label='二氧化硫',color="tab:blue",linewidth=18) rects2=ax.bar(x-width/2.0,y2 , width, bottom=y1,label='氮氧化物',color="tab:orange") rects3=ax.bar(x-width/2.0,y3 , width, bottom=y2+y1,label='煙塵',color="tab:green") rects11=ax.bar(x+width/2.0+0.04, y21, width, label='二氧化硫',color="tab:blue") rects22=ax.bar(x+width/2.0+0.04,y22 , width, bottom=y21,label='氮氧化物',color="tab:orange") rects33=ax.bar(x+width/2.0+0.04,y23 , width, bottom=y22+y21, label='煙塵',color="tab:green") ax.set_ylabel('噸') title='2018-2019年各地市排放情況(%s)'%regionType ax.set_title(title) ax.legend([rects1,rects2,rects3],['二氧化硫', '氮氧化物', '煙塵']) plt.xticks(x,labels, rotation=45) yy=np.array([y1,y2,y3]) yy2=yy.cumsum(0) yyy=np.array([y21,y22,y23]) yyy2=yyy.cumsum(0) fontProperties=FontProperties(size=6) def autolabel1(rects,rectIndex): """Attach a text label above each bar in *rects*, displaying its height.""" for i,rect in enumerate(rects): height = (yy2[rectIndex-1][i] if rectIndex >0 else 0)+yy[rectIndex][i]/2.0 value=yy[rectIndex][i] ax.annotate('{}'.format(value), xy=(rect.get_x() + rect.get_width() / 2, height-200), xytext=(0, 3), # 3 points vertical offset textcoords="offset points",fontproperties=fontProperties, ha='center', va='bottom') def autolabel2(rects,rectIndex): """Attach a text label above each bar in *rects*, displaying its height.""" for i,rect in enumerate(rects): height = (yyy2[rectIndex-1][i] if rectIndex >0 else 0)+yyy[rectIndex][i]/2.0 value=yyy[rectIndex][i] ax.annotate('{}'.format(value), xy=(rect.get_x() + rect.get_width() / 2, height-200), xytext=(0, 3), # 3 points vertical offset textcoords="offset points",fontproperties=fontProperties, ha='center', va='bottom') autolabel1(rects1,0) autolabel1(rects2,1) autolabel1(rects3,2) autolabel2(rects11,0) autolabel2(rects22,1) autolabel2(rects33,2) plt.savefig(title+'.png', dpi=200) for regionType in regionTypes: print(regionType) getPlot(regionType)