matplotlib 棒グラフ
概要
シンプルな棒グラフ、2軸棒グラフ、積み上げ棒グラフの紹介をします。 シンプルな棒グラフ、2軸棒グラフでは、白黒出力対応で、棒の中をハッチングしています
シンプルな棒グラフ
# Bar chart import matplotlib.pyplot as plt import numpy as np def main(): # data for plot y1=[8127.220, 3109.375, 3683.840, 6504.671, 11687.447, 13068.109, 3993.506, 5995.114, 7050.279, 4058.274] y2=[1989.080, 2133.271, 4057.118, 5238.903, 10335.058, 11830.916, 3870.442, 5212.632, 5389.747, 2943.048] ss= ['B-C','B-1','B-2','B-3','B-A','B-P','C-E','C-I','B-W','C-W'] x = np.arange(len(y1)) # x-coordinate w = 0.3 # width of a bar fsz=16 plt.figure(figsize=(12,6),facecolor='w') plt.rcParams["font.size"]=fsz plt.rcParams['font.family']='sans-serif' plt.ylim([0,15000]) plt.ylabel('Max. bending moment (kN-m)') plt.xlabel('Member name') plt.xticks(x+w/2,ss,rotation=60) plt.bar(x, y1,width=w,label='with crane load', align='center',lw=1,fill=None,hatch='xx') plt.bar(x+w,y2,width=w,label='without crne load',align='center',lw=1,fill=None,hatch='/') plt.legend(loc='best',shadow=True) fnameF='fig_bar1.jpg' plt.savefig(fnameF, dpi=100, bbox_inches="tight", pad_inches=0.1) plt.show() #--------------- # Execute #--------------- if __name__ == '__main__': main()
2軸棒グラフ
# Bar chart with 2 axes import matplotlib.pyplot as plt import numpy as np def main(): # data for plot site=np.array(['L1','K ','M ','Q ','K1 ','J ','J1','G ','H ','I ']) hdam=np.array([ 189, 108, 94, 186, 145, 138, 169, 108, 95, 90]) lway=np.array([2304,4312,4300,2195,3999,3808,2987,3193,5034,5838]) # sort of data ii=np.argsort(hdam) sp=site[ii] hp=hdam[ii] lp=lway[ii] x=np.arange(len(sp)) # x-coordinate w=0.3 # width of a bar fsz=16 plt.figure(figsize=(12,6),facecolor='w') plt.rcParams["font.size"] = fsz # drawing on left hand side axis plt.ylim(0,300) plt.ylabel('Dam height (m)') plt.xlabel('Site name') plt.xticks(x+w/2,sp,rotation=0) plt.grid(color='#999999',linestyle='--') plt.bar(x,hp,width=w,align='center',label='Dam height (m)',fill=None,hatch='xx') # drawing on right handnside axis plt.twinx() plt.ylim(0,6000) plt.ylabel('Waterway length (m)') plt.bar(x+w,lp,width=w,align='center',label='Waterway length (m)',fill=None,hatch='/') plt.bar([0],[0],width=w,align='center',label='Dam height (m)',fill=None,hatch='xx') # dummy for legend # drawing of legend plt.legend(loc='best',shadow=True) fnameF='fig_bar2.jpg' plt.savefig(fnameF, dpi=100, bbox_inches="tight", pad_inches=0.1) plt.show() #--------------- # Execute #--------------- if __name__ == '__main__': main()
積み上げ棒グラフ
import matplotlib.pyplot as plt import numpy as np def main(): stext=[] stext=stext+['Site-L1\nCase-1'] stext=stext+['Site-L1\nCase-2'] stext=stext+['Site-L1\nCase-3'] stext=stext+['Site-I\nCase-1'] stext=stext+['Site-I\nCase-2'] stext=stext+['Site-I\nCase-3'] wway=np.array([ 430, 628, 455, 622, 841, 570]) # Power waterway ures=np.array([2235,2235,1118, 456, 456, 228]) # upper reservoir coth=np.array([ 371, 371, 186, 358, 358, 179]) # civil others eadm=np.array([ 419, 463, 463, 420, 464, 464]) # E&M tsum=np.array([3754,3996,2491,2154,2409,1667]) # total oths=tsum-eadm-coth-ures-wway # others xx=np.array([1,2,3,5,6,7]) cef=1000 fsz=14 plt.figure(figsize=(7,7),facecolor='w') plt.rcParams["font.size"] = fsz plt.subplot(111) plt.ylim(0,5000/cef) plt.xlabel('Case') plt.ylabel('Cost Index') plt.xticks(xx,stext,rotation=60) plt.grid(color='#999999',linestyle='--') # drawing bars b0=np.array([0,0,0,0,0,0]) b1=b0+oths/cef b2=b1+eadm/cef b3=b2+coth/cef b4=b3+ures/cef plt.bar(xx,wway/cef,bottom=b4,width=0.8,align='center',label='Waterway') plt.bar(xx,ures/cef,bottom=b3,width=0.8,align='center',label='Upper Res.') plt.bar(xx,coth/cef,bottom=b2,width=0.8,align='center',label='Civil_others') plt.bar(xx,eadm/cef,bottom=b1,width=0.8,align='center',label='E&M') plt.bar(xx,oths/cef,bottom=b0,width=0.8,align='center',label='Others') plt.legend(shadow=True,loc='upper right') plt.title('Comparison of Cost Index between Site-L1 and Site-I',loc='left',fontsize=fsz-1) fnameF='fig_bar3.png' plt.tight_layout() plt.savefig(fnameF, dpi=100, bbox_inches="tight", pad_inches=0) plt.show() #--------------- # Execute #--------------- if __name__ == '__main__': main()