Python绘制并列柱状图

Python绘制并列柱状图,第1张

概述相关知识绘制堆积柱状图bar函数调用方式如下所示:matplotlib.pyplot.bar(x,height,width=0.8,bottom=None,*,align='center',data=None,**kwargs) 要想绘制堆积柱状图,可通过设置第一个参数x的值来使得柱形错位显示,x的每一个元素表示柱形的中间位置,示例代码如下所示: 相关知识

绘制堆积柱状图

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

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

 

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

import numpy as npimport 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 pltimport 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 = 6ystring = ['']*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 **********#

案例代码运行结果

 

总结

以上是内存溢出为你收集整理的Python绘制并列柱状图全部内容,希望文章能够帮你解决Python绘制并列柱状图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1185226.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存