本節(jié)主要分2個內(nèi)容:1. 實現(xiàn)對柱狀圖添加誤差棒慧妄; 2. 實現(xiàn)并列型帶誤差棒柱狀圖
1. 實現(xiàn)對柱狀圖添加誤差棒戳护,bar()篇內(nèi)容婉刀,官方bar()文檔匀钧,柱狀圖參數(shù)詳情
補充參數(shù)信息
xerr, yerr: 分別針對水平、垂直型誤差
error_kw: 設(shè)置誤差記號的相關(guān)參數(shù)槽唾,包括elinewidth設(shè)置線型粗細(xì)丧枪,ecolor設(shè)置顏色,capsize設(shè)置頂部橫線大小
最簡的實現(xiàn)
import matplotlib.pyplot as plt
x=[1,2,3,4,5]
#數(shù)據(jù)集
y=[20,44,21,64,46]
#誤差列表
std_err=[1,2,5,3,2]
error_params=dict(elinewidth=4,ecolor='coral',capsize=5)#設(shè)置誤差標(biāo)記參數(shù)
#繪制柱狀圖庞萍,設(shè)置誤差標(biāo)記以及柱狀圖標(biāo)簽
plt.bar(x,y,color=['b','g','yellow','orange','gray'],yerr=std_err,error_kw=error_params,\
tick_label=['blue','green','yellow','orange','gray'])
#顯示圖形
plt.show()
顯示效果
2. 并列型帶誤差棒柱狀圖
實現(xiàn)思路:1. 兩個數(shù)據(jù)集债沮,兩個誤差棒;2. 分別對誤差棒進(jìn)行設(shè)置本鸣。
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(5)
#數(shù)據(jù)集
y1=[20,44,21,64,46]
y2=[10,37,24,41,40]
#誤差列表
std_err1=[1,2,5,3,2]
std_err2=[2,4,3,1,2]
tick_label=['blue','green','yellow','orange','gray']
error_params1=dict(elinewidth=3,ecolor='crimson',capsize=4)#設(shè)置誤差標(biāo)記參數(shù)
error_params2=dict(elinewidth=3,ecolor='blueviolet',capsize=4)#設(shè)置誤差標(biāo)記參數(shù)
#設(shè)置柱狀圖寬度
bar_width=0.4
#繪制柱狀圖疫衩,設(shè)置誤差標(biāo)記以及柱狀圖標(biāo)簽
plt.bar(x,y1,bar_width,color=['b','g','yellow','orange','gray'],yerr=std_err1,error_kw=error_params1,label='tag A')
plt.bar(x+bar_width,y2,bar_width,color=['b','g','yellow','orange','gray'],yerr=std_err1,error_kw=error_params2,label='tag B')
plt.xticks(x+bar_width/2,tick_label)#設(shè)置x軸的標(biāo)簽
#設(shè)置網(wǎng)格
plt.grid(True,axis='y',ls=':',color='r',alpha=0.3)
#顯示圖例
plt.legend()
#顯示圖形
plt.show()
效果如下