天天看點

matplotlib 柱狀圖 Bar Chart 樣例及參數

def bar_chart_generator():

l = [1,2,3,4,5]

h = [20, 14, 38, 27, 9]

w = [0.1, 0.2, 0.3, 0.4, 0.5]

b = [1,2,3,4,5]

fig = plt.figure()
ax = fig.add_subplot(111)
rects = ax.bar(l, h, w, b,
               color='#ffff00',
               edgecolor='#000000',
               linewidth=2,
               #xerr=4,
               #yerr=1,
               #ecolor='#999999',
               #capsize=10,
               #align='center',
               #orientation='horizontal',
              )
plt.savefig('bar.png')
           

==================================

函數原型:

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

基本參數:

left 每個柱x軸左邊界

bottom 每個柱y軸下邊界

height 柱高度(Y軸方向)

width 柱寬度(X軸方向)

以上參數可以設定為數值或者list

但要保證如果為list, len(list)要一緻

繪制的方形為:

X: left — left+width

Y: bottom — bottom+height

傳回值:

matplotlib.patches.Rectangle

柱狀圖使用bottom擴充即可化為甘特圖 Gantt Chart

其他參數:

color Bar顔色

edgecolor Bar邊界線顔色

align 可選[‘left’(default) | ‘center’]

決定整個bar圖分布

預設left表示預設從左邊界開始繪制,center會将圖繪制在中間位置

xerr x方向error bar

yerr y方向error bar

ecolor error bar顔色

capsize error bar橫線寬度(default 3)

樣例:

基本設定

l = [1,2,3,4,5]

h = [20, 14, 38, 27, 9]

w = [0.1, 0.2, 0.3, 0.4, 0.5]

b = [1,2,3,4,5]

matplotlib 柱狀圖 Bar Chart 樣例及參數

設定xerr=4

matplotlib 柱狀圖 Bar Chart 樣例及參數

設定yerr=1,ecolor=’#999999’

matplotlib 柱狀圖 Bar Chart 樣例及參數