matplotlib手册(5) - 柱状图

浏览: 3008

这里主要整理下matplotlib中绘制柱状图的方法,参考了几篇文章:

matplotlib绘图——柱状图

官网介绍

感谢上面的分享,博客写的很好很全,帮助很大,下面我们开始。

我们要绘制柱形图,要使用pyplot的bar方法

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

Make a bar plot.

Make a bar plot with rectangles bounded by:

left, left + width, bottom, bottom + height
(left, right, bottom and top edges)

做了一些练习之后,发现通过left、height、width、bottom这几个参数可以实现很多不同的图表,后面我们会说到。

因为是柱形图,所以四个点的坐标很重要,我们初始化,必须指定的是left和height,width默认是0.8,bottom默认是None,就是0

下面,开始我们的小例子

1.基本的柱形图

import matplotlib.pyplot as plt

#plt.bar(left=[10,11],height=[10,15])
plt.bar([10,11],[10,15])

plt.grid(True)
plt.show()

图片.png

上面就是一个简单的柱状图,我们来分析一下,这个图

width&bottom

我们传入的参数是left=[10,11],height=[10,15],以第一个柱状图为例,他四个点的坐标为:图展示的时候,默认是居中的

左下(left-width/2,bottom),右下(left+width/2,bottom),左上(left-width/2,bottom+height),右上(left+width/2,bottom+height)

即(9.6,0),(10.4,0),(9.6,10),(10.4,10),

理解这个位置很重要,后面会用的到。

下面,我们就修改下参数,加深下这个位置的理解

#我们把width改为1,看起来会更方便
plt.bar([10,12],[10,15],width=1)

图片.png

我们可以修改对齐方式

#align : {‘center’, ‘edge’}, optional
#If ‘edge’, aligns bars by their left edges (for vertical bars) and by their bottom edges (for horizontal bars).

plt.bar([10,12],[10,15],width=1,align='edge')

图片.png

我们看看width和bottom的参数介绍,他们都可以是一个常量,或数组的,什么意思呢?就是说,我们可以给不同的柱状图设置不同的宽度和y轴起始高度

width : scalar or array-like, optional
bottom : scalar or array-like, optional

像这样:

plt.bar([10,12],[10,15],width=[0.5,1],bottom=[-5,0])

图片.png


好了,下面,我们就开始看看一些常用的参数

颜色

facecolor or fc: mpl color spec, or None for default, or ‘none’ for no color
color : Set both the edgecolor and the facecolor
import matplotlib.pyplot as plt

#设置填充颜色和边框颜色
plt.bar([10,12,14],[10,15,20],facecolor='yellow',edgecolor='blue')

plt.show()

图片.png

那个color也可以设置填充颜色和边框颜色,但是他比较好玩儿,他可以接受一个颜色数组,让不同的柱状图颜色不一样

plt.bar([10,12,14],[10,15,20],color=['red','green','blue'])

图片.png

边框的样式和宽度

linestyle or ls : [‘solid’ | ‘dashed’, ‘dashdot’, ‘dotted’ | (offset, on-off-dash-seq) | '-' | '--' | '-.' | ':' | 'None' | ' ' | '']
linewidth or lw : float or None for default
import matplotlib.pyplot as plt

plt.bar([10,12,14],[10,15,20],facecolor='yellow',edgecolor='blue',linestyle='--',linewidth=3)

plt.show()

图片.png

填充

hatch: 
/ - diagonal hatching
\ - back diagonal
| - vertical
- - horizontal
+ - crossed
x - crossed diagonal
o - small circle
O - large circle
. - dots
* - stars

Letters can be combined, in which case all the specified hatchings are done. If same letter repeats, it increases the density of hatching of that pattern.
plt.bar([10,12,14],[10,15,20],facecolor='yellow',edgecolor='blue'
,linestyle='--',linewidth=3,hatch='/')

图片.png

这个hatch是可以组合的

plt.bar([10,12,14],[10,15,20],facecolor='yellow',edgecolor='blue'
,linestyle='--',linewidth=3,hatch='/\\')

图片.png


标签

下面还有一个参数,我们平时使用时,x轴可能并不是数字,而是文字,这个也是可以的,

tick_label : string or array-like, optional
the tick labels of the bars default: None
import matplotlib.pyplot as plt

plt.bar([10,12,14],[10,15,20],facecolor='yellow',edgecolor='blue'
,linestyle='--',linewidth=3,hatch='/\\',tick_label=['one','two','three'])

plt.show()

图片.png

这个中文标签,暂时还不行,会显示乱码,这个bar貌似没有字体参数,等我研究下,再回来分享。

好辣,中文是可以处理的,加上参数就行了,详情参考:matplotlib手册(4)-中文乱码 中最下面的更新的方法

2. 堆叠柱状图

就是根据坐标,将2个柱状图上下放置即可,就是灵活运用left、bottom、width、height参数

import matplotlib.pyplot as plt


plt.bar([10,12,14],[10,15,20],facecolor='green',tick_label=['one','two','three'],label='green')
#横坐标一样就行了,
plt.bar([10,12,14],[5,30,10],bottom=[10,15,20],facecolor='orange',label='orange')

plt.legend()

plt.show()

图片.png


3. 并列柱状图

  就是同时显示过个柱状图,同样是利用坐标,计算好位置就行了,将右边的left加上一个width就行了,

import matplotlib.pyplot as plt

common_width=1
plt.bar([10,20,30],[10,15,20],width=common_width,facecolor='red',label='red')
#只需要将left+width就行了
plt.bar([11,21,31],[5,20,10],width=common_width,facecolor='green',label='green')

plt.legend()

plt.show()

图片.png

推荐 0
本文由 liutangwow 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册