相關知識
繪制堆積柱狀圖
bar函數調用方式如下所示:
matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)
要想繪制堆積柱狀圖,可通過設定第一個參數x的值來使得柱形錯位顯示,x的每一個元素表示柱形的中間位置,示例代碼如下所示:
- import numpy as np
- import matplotlib.pyplot as plt
- # A班計算機程式設計課5個小組的平均成績柱狀圖
- A_means_score = np.array([90, 85, 77, 82, 79])
- # B班計算機程式設計課5個小組的平均成績柱狀圖
- B_means_score = np.array([67, 82, 87, 92, 95])
- index = np.arange(5)
- bar_width = 0.35
- plt.bar(index, A_means_score, bar_width, # A班x軸資料起始位置為index序列
- alpha=0.4, color='b')
- plt.bar(index+bar_width, B_means_score, bar_width, #B班x軸起始位置與A班資料錯開
- alpha=0.4, color='r')
- x_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4', 'Group 5']
- plt.xticks(index+bar_width/2, x_labels) # index+bar_width/2 使得标簽居中顯示
- plt.show()
輸出圖像如下所示:

若是有多組資料,則可通過結合清單以及循環結構進行繪圖控制。
案例代碼示例
# 請編寫代碼繪制住宅商品房平均銷售價格柱狀圖
import matplotlib.pyplot as plt
import numpy as np
#初始化資料
xstring = [2015,2014,2013,2012,2011,2010,2009,2008,2007,2006,2005,2004,2003,2002,2001,2000] #x軸标簽
xstring.reverse()
xstrinf = tuple(xstring)
n = 6
ystring = ['']*n #y軸對應的6組資料
ystring[0] = [6793,6324,6237,5790.99,5357.1,5032,4681,3800,3863.9,3366.79,3167.66,2778,2359,2250,2170,2112]
ystring[1] = [6473,5933,5850,5429.93,4993.17,4725,4459,3576,3645.18,3119.25,2936.96,2608,2197,2092,2017,1948]
ystring[2] = [15157,12965,12591,11460.19,10993.92,10934,9662,7801,7471.25,6584.93,5833.95,5576,4145,4154,4348,4288]
ystring[3] = [12914,11826,12997,12306.41,12327.28,11406,10608,8378,8667.02,8052.78,6922.52,5744,4196,4336,4588,4751]
ystring[4] = [9566,9817,9777,9020.91,8488.21,7747,6871,5886,5773.83,5246.62,5021.75,3884,3675.14,3488.57,3273.53,3260.38]
ystring[5] = [4845,5177,4907,4305.73,4182.11,4099,3671,3219,3351.44,3131.31,2829.35,2235,2240.74,1918.83,2033.08,1864.37]
ystring[0].reverse()
ystring[1].reverse()
ystring[2].reverse()
ystring[3].reverse()
ystring[4].reverse()
ystring[5].reverse()
ystring[0] = tuple(ystring[0])
ystring[1] = tuple(ystring[1])
ystring[2] = tuple(ystring[2])
ystring[3] = tuple(ystring[3])
ystring[4] = tuple(ystring[4])
ystring[5] = tuple(ystring[5])
labels = ['Commercial housing', 'Residential commercial housing','high-end apartments', 'Office Building', 'Business housing', 'Others'] #圖例标簽
colors = ['#ff7f50', '#87cefa', '#DA70D6', '#32CD32', '#6495ED', '#FF69B4'] #指定顔色
# ********** Begin *********#
A_means_score = np.array(ystring[0])
B_means_score = np.array(ystring[1])
C_means_score = np.array(ystring[2])
D_means_score = np.array(ystring[3])
E_means_score = np.array(ystring[4])
F_means_score = np.array(ystring[5])
index = np.arange(16)
bar_width = 0.8#設定單個柱狀圖寬度
#繪制圖像
plt.bar(6*index,A_means_score,bar_width,color = colors[0])
plt.bar(6*index+bar_width,B_means_score,bar_width,color = colors[1])
plt.bar(6*index+2*bar_width,C_means_score,bar_width,color = colors[2])
plt.bar(6*index+3*bar_width,D_means_score,bar_width,color = colors[3])
plt.bar(6*index+4*bar_width,E_means_score,bar_width,color = colors[4])
plt.bar(6*index+5*bar_width,F_means_score,bar_width,color = colors[5])
#設定x軸範圍及其刻度屬性
plt.xlim(-1,98)
plt.xticks(6*index+(bar_width+bar_width+bar_width+bar_width+bar_width)/2,xstring,rotation=45)
#設定y軸範圍及其間隔
plt.ylim(1450,15300)
plt.yticks(np.arange(1450,15300,2000))
#設定圖例及标題屬性
plt.legend(labels,loc='upper left')
plt.title('Selling Prices of Six Types of Housing')
plt.savefig('picture/step2/fig2.png')
plt.show()
# ********** End **********#
案例代碼運作結果