matplotlib作图系列之bar柱状图

函数

>matplotlib.pyplot.bar(left,height,width=0.8,bottom=None,hold=None,**kwargs)

举例

对一组原始数据(三组平行重复)
a1=4.62,a2=3.73,a3=4.01
b1=5.84,b2=5.88,b3=5.96
c1=2.58,c2=1.88,c3=1.70
d1=1.52,d2=2.94,d3=1.33
e1=3.73,e2=3.99,e3=5.51
绘制带误差线的柱状图

import numpy as np
import matplotlib.pyplot as plt
#a=[4.62,3.73,4.01]
#b=[5.84,5.88,5.96]
#c=[2.58,1.88,1.70]
#d=[1.52,2.94,1.33]
#e=[3.73,3.99,5.51]
#means=[np.mean(a),np.mean(b),np.mean(c),np.mean(d),np.mean(e)]#平均值
means=[4.12,5.89,2.05,1.93,4.41]#平均值
#a_err=np.sqrt((np.square(a[0]-np.mean(a))+np.square(a[1]-np.mean(a))+np.square(a[2]-np.mean(a)))/(3*2))
#b_err=np.sqrt((np.square(b[0]-np.mean(b))+np.square(b[1]-np.mean(b))+np.square(b[2]-np.mean(b)))/(3*2))
#c_err=np.sqrt((np.square(c[0]-np.mean(c))+np.square(c[1]-np.mean(c))+np.square(c[2]-np.mean(c)))/(3*2))
#d_err=np.sqrt((np.square(d[0]-np.mean(d))+np.square(d[1]-np.mean(d))+np.square(d[2]-np.mean(d)))/(3*2))
#e_err=np.sqrt((np.square(e[0]-np.mean(e))+np.square(e[1]-np.mean(e))+np.square(e[2]-np.mean(e)))/(3*2))
#err=[a_err,b_err,c_err,d_err,e_err]
err=[0.26,0.04,0.27,0.51,0.56]#标准误
plt.figure(figsize=(6,6),dpi=80)#设置画布
plt.ylim((0,6))#设置y轴范围
index=np.arange(5)#共5个组
plt.bar(index,means,color=['olivedrab','darkorange','chocolate','dodgerblue','turquoise'],yerr=err,error_kw={'ecolor':'0.2','capsize':6},alpha=0.7,label='First')
plt.xticks(index,['a','b','c','d','e'])#设置五个index对应的名字
plt.xlabel('group')#设置x轴注释
plt.ylabel('value')#设置y轴注释
plt.title('bar')#设置标题
plt.savefig('bar.pdf')#保存
plt.show()

结果如下:
matplotlib作图系列之bar柱状图_第1张图片

参数介绍

基本参数:
(1)left,可以理解为index的意思,就是一个柱状图中有几个柱子
(2)height,各个组对应的柱状图的高度
(3)width,柱状图中每个柱子的宽度
详细参数见下表:
matplotlib作图系列之bar柱状图_第2张图片
matplotlib作图系列之bar柱状图_第3张图片
matplotlib作图系列之bar柱状图_第4张图片
matplotlib作图系列之bar柱状图_第5张图片

你可能感兴趣的:(可视化,python)