signed

QiShunwang

“诚信为本、客户至上”

Python绘制并列柱状图

2021/6/3 17:54:34   来源:

相关知识

为了完成本关任务,你需要掌握绘制堆积(并列)柱状图。

绘制堆积柱状图

bar函数调用方式如下所示:

matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)

 

要想绘制堆积柱状图,可通过设置第一个参数x的值来使得柱形错位显示,x的每一个元素表示柱形的中间位置,示例代码如下所示:

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. # A班计算机程序设计课5个小组的平均成绩柱状图
  5. A_means_score = np.array([90, 85, 77, 82, 79])
  6.  
  7. # B班计算机程序设计课5个小组的平均成绩柱状图
  8. B_means_score = np.array([67, 82, 87, 92, 95])
  9.  
  10. index = np.arange(5)
  11. bar_width = 0.35
  12.  
  13. plt.bar(index, A_means_score, bar_width, # Ax轴数据起始位置为index序列
  14.                 alpha=0.4, color='b')
  15. plt.bar(index+bar_width, B_means_score, bar_width, #Bx轴起始位置与A班数据错开
  16.                 alpha=0.4, color='r')
  17. x_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4', 'Group 5']
  18. plt.xticks(index+bar_width/2, x_labels) # index+bar_width/2 使得标签居中显示
  19. 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 **********#

案例代码运行结果