Python中使用matplotlib模塊errorbar函數(shù)繪制誤差棒圖實例代碼
Python的matplotlib模塊中的errorbar函數(shù)可以繪制誤差棒圖,本次主要繪制不帶折線的誤差棒圖。
1.基本參數(shù)
errorbar函數(shù)的基本參數(shù)主要有:
x,y:主要定于二維數(shù)據(jù)的橫縱坐標值
yerr :定義y軸方向的誤差棒的大小,可以是一個數(shù),也可以是二維數(shù)組(分別傳遞平均值與最小值的差和最大值與平均值的差)。
xerr:定義y軸方向的誤差棒的大小,同樣也可以是一個數(shù),也可以是二維數(shù)組。
fmt:定義數(shù)據(jù)折線和數(shù)據(jù)點的樣式。
ecolor:定義誤差棒的顏色。
elinewidth:定義誤差棒線的寬度。
capsize:定義誤差棒帽的大?。ㄩL度)。
capthick:定義誤差棒帽的寬度。
alpha:設置透明度(范圍:0-1)。
marker:設置數(shù)據(jù)點的樣式(具體字母代表的樣式可以參考:matplotlib.marker)。

markersize(簡寫ms):定義數(shù)據(jù)點的大小。
markeredgecolor(簡寫mec):定義數(shù)據(jù)點的邊的顏色,可使用官方提供的縮寫字母代表的簡單顏色,也可以使用RGB顏色和HTML十六進制#aaaaaa格式的顏色(具體可參考matplotlib.colors)。
markeredgewidth( 簡寫mew ):定義數(shù)據(jù)點的邊的寬度。
markerfacecolor(簡寫 mfc):定義數(shù)據(jù)點的顏色。
linestyle:設置折線的樣式,設置成none可將折線隱藏。
label:添加圖例。
2.代碼實現(xiàn)
#導入函數(shù)庫
import matplotlib.pylab as plt
import numpy as np
#繪制誤差棒圖
plt.figure(1)
#將數(shù)據(jù)導入
#導入最小值、最大值
obs_min,obs_max = np.loadtxt('obs_syn_amp_mean.dat', usecols=(8,9), unpack=True)
#導入x以及平均值
x,obs_mean = np.loadtxt('obs_syn_amp_mean.dat', usecols=(1,10), unpack=True)
#設置errorbar的大小
yerr = np.zeros([2,len(obs_mean)])
yerr[0,:] = obs_mean - obs_min
yerr[1,:] = obs_max - obs_mean
#繪制errorbar
plt.errorbar(x,obs_mean,yerr=yerr[:,:],ecolor='k',elinewidth=0.5,marker='s',mfc='orange',\
mec='k',mew=1,ms=10,alpha=1,capsize=5,capthick=3,linestyle="none",label="Observation")
# 設置坐標軸及圖例顯示信息
plt.xlabel(r"Distance $(\degree)$", fontsize=15)
plt.ylabel(r"Amplitude Ratio", fontsize=15)
plt.xticks(fontsize=10)
plt.yticks(fontsize=10)
plt.legend(fontsize=15)
# 將圖片保存在當前目錄
fig = plt.gcf()
fig.set_size_inches(8, 10)
fig.savefig('Obs-syn-amp-mean.png', dpi=500)
plt.close()
3.結果顯示

4.更多參數(shù)請參考matplotlib官網(wǎng)
總結
到此這篇關于Python中使用matplotlib模塊errorbar函數(shù)繪制誤差棒圖的文章就介紹到這了,更多相關Python errorbar函數(shù)繪制誤差棒圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

