目录
基本用法
多个直方图并列显示
显示直方图上的数值
多个直方图堆叠显示
水平直方图
相较散点图和折线图,柱状图(直方图、条形图)、饼图、箱线图是另外 3 种数据分析常用的图形,主要用于分析数据内部的分布状态或分散状态。
柱状图(直方图、条形图)主要用于查看各分组数据的数量分布,以及各个分组数据之间的数量比较。
Matplotlib 中绘制柱状图(直方图、条形图)的函数为 bar() ,使用语法如下:
atplotlib.pyplot.bar(left, height, width=0.8, bottom=None, hold=None, data=None, **kwargs)
常用参数及说明:
bar() 其他参数请参考文档:/api/pyplot_api.html
基本用法
import pandas as pdimport matplotlib.pyplot as plt#读取数据datafile = u'D:\\pythondata\\learn\\matplotlib.xlsx'data = pd.read_excel(datafile)plt.figure(figsize=(10,5))#设置画布的尺寸plt.title('Examples of Histogram',fontsize=20)#标题,并设定字号大小plt.xlabel(u'x-year',fontsize=14)#设置x轴,并设定字号大小plt.ylabel(u'y-income',fontsize=14)#设置y轴,并设定字号大小#alpha:透明度;width:柱子的宽度;facecolor:柱子填充色;edgecolor:柱子轮廓色;lw:柱子轮廓的宽度;label:图例;plt.bar(data['时间'],data['收入_Jay'], alpha=0.6,width = 0.8, facecolor = 'deeppink', edgecolor = 'darkblue', lw=1, label='Jay income')plt.legend(loc=2)#图例展示位置,数字代表第几象限plt.show()#显示图像
多个直方图并列显示
通过bar() 中的 width 参数来调节:
import pandas as pdimport matplotlib.pyplot as plt#读取数据datafile = u'D:\\pythondata\\learn\\matplotlib.xlsx'data = pd.read_excel(datafile)plt.figure(figsize=(10,5))#设置画布的尺寸plt.title('Examples of Histogram',fontsize=20)#标题,并设定字号大小plt.xlabel(u'x-year',fontsize=14)#设置x轴,并设定字号大小plt.ylabel(u'y-income',fontsize=14)#设置y轴,并设定字号大小width_val = 0.4 #若显示 n 个柱状图,则width_val的值需小于1/n ,否则柱形图会有重合#alpha:透明度;width:柱子的宽度;facecolor:柱子填充色;edgecolor:柱子轮廓色;lw:柱子轮廓的宽度;label:图例;plt.bar(data['时间'],data['收入_Jay'], alpha=0.6,width = width_val, facecolor = 'deeppink', edgecolor = 'deeppink', lw=1, label='Jay income')plt.bar(data['时间']+width_val,data['收入_JJ'], alpha=0.6,width = width_val, facecolor = 'darkblue', edgecolor = 'darkblue', lw=1, label='JJ income')plt.legend(loc=2)#图例展示位置,数字代表第几象限plt.show()#显示图像
显示直方图上的数值
import pandas as pdimport matplotlib.pyplot as plt#读取数据datafile = u'D:\\pythondata\\learn\\matplotlib.xlsx'data = pd.read_excel(datafile)plt.figure(figsize=(10,5))#设置画布的尺寸plt.title('Examples of Histogram',fontsize=20)#标题,并设定字号大小plt.xlabel(u'x-year',fontsize=14)#设置x轴,并设定字号大小plt.ylabel(u'y-income',fontsize=14)#设置y轴,并设定字号大小width_val = 0.4 #若显示 n 个柱状图,则width_val的值需小于1/n ,否则柱形图会有重合#alpha:透明度;width:柱子的宽度;facecolor:柱子填充色;edgecolor:柱子轮廓色;lw:柱子轮廓的宽度;label:图例;rects_Jay = plt.bar(data['时间'],data['收入_Jay'], alpha=0.6,width = width_val, facecolor = 'deeppink', label='Jay income')rects_JJ = plt.bar(data['时间']+width_val,data['收入_JJ'], alpha=0.6,width = width_val, facecolor = 'darkblue',label='JJ income')# 添加数据标签 就是矩形上面的数值def add_labels(rects):for rect in rects:height = rect.get_height()plt.text(rect.get_x() + rect.get_width()/2, height, height, ha='center', va='bottom')rect.set_edgecolor('white')add_labels(rects_Jay)add_labels(rects_JJ)plt.legend(loc=2)#图例展示位置,数字代表第几象限plt.show()#显示图像
多个直方图堆叠显示
通过bar() 中的 bottom 参数来调节:
import pandas as pdimport matplotlib.pyplot as plt#读取数据datafile = u'D:\\pythondata\\learn\\matplotlib.xlsx'data = pd.read_excel(datafile)plt.figure(figsize=(10,5))#设置画布的尺寸plt.title('Examples of Histogram',fontsize=20)#标题,并设定字号大小plt.xlabel(u'x-year',fontsize=14)#设置x轴,并设定字号大小plt.ylabel(u'y-income',fontsize=14)#设置y轴,并设定字号大小#通过bottom使得两个柱状图堆叠显示,且没有交叉#alpha:透明度;width:柱子的宽度;facecolor:柱子填充色;edgecolor:柱子轮廓色;lw:柱子轮廓的宽度;label:图例;plt.bar(data['时间'],data['收入_Jay'], alpha=0.6,width = width_val, facecolor = 'deeppink', edgecolor = 'deeppink', lw=1, label='Jay income')plt.bar(data['时间'],data['收入_JJ'], bottom=data['收入_Jay'], alpha=0.6,width = width_val, facecolor = 'darkblue', edgecolor = 'darkblue', lw=1, label='JJ income')plt.legend(loc=2)#图例展示位置,数字代表第几象限plt.show()#显示图像
水平直方图
通过 barh() 函数实现,与 bar() 函数的主要区别是:在bar() 函数中,width 这一参数代表的是柱子的宽度(胖瘦),而在barh() 函数中width 这一参数代表的是横向柱子的长度(长短),在下面代码中,width =data['收入_Jay']。
Matplotlib 中绘制水平柱状图(直方图、条形图)的函数为 barh() ,使用语法如下:
matplotlib.pyplot.
barh
(y,width,height=0.8,left=None,*,align='center',**kwargs)
参数请参考文档:/api/_as_gen/matplotlib.pyplot.barh.html?highlight=barh#matplotlib.pyplot.barh
import pandas as pdimport matplotlib.pyplot as plt#读取数据datafile = u'D:\\pythondata\\learn\\matplotlib.xlsx'data = pd.read_excel(datafile)plt.figure(figsize=(10,5))#设置画布的尺寸plt.title('Examples of Histogram',fontsize=20)#标题,并设定字号大小plt.xlabel(u'x-year',fontsize=14)#设置x轴,并设定字号大小plt.ylabel(u'y-income',fontsize=14)#设置y轴,并设定字号大小#alpha:透明度;facecolor:柱子填充色;edgecolor:柱子轮廓色;lw:柱子轮廓的宽度;label:图例;plt.barh(data['时间'],data['收入_Jay'], alpha=0.6, facecolor = 'deeppink', edgecolor = 'deeppink', label='Jay income')plt.legend(loc=4)#图例展示位置,数字代表第几象限plt.show()#显示图像
如果还有直方图的其他花样,欢迎留言,我们一起探讨。