现在的位置: 首页 > 综合 > 正文

Python画图工具matplotlib的使用(图形并茂)

2018年04月22日 ⁄ 综合 ⁄ 共 4600字 ⁄ 字号 评论关闭

matplotlib官网文档:

http://matplotlib.org/gallery.html

支持win7_64_bit的matplotlib库下载网址:

http://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib

 

简介:

          matplotlib 是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图。而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中。它的文档相当完备,并且 Gallery页面 中有上百幅缩略图,打开之后都有源程序。因此如果你需要绘制某种类型的图,只需要在这个页面中浏览/复制/粘贴一下,基本上都能搞定。

           这篇我们用matplotlib从构造最简单的bar一步一步向复杂的bar前行。什么是最简单的bar,看如下语句你就知道她有多么简单了:

参考代码:

>>> print os.getcwd()

D:\Python27

>>> import matplotlib.pyplot as plt

>>> plt.bar(left = 0,height = 1)

<Container object of 1 artists>

>>> plt.show()

首先我们import了matplotlib.pyplot ,然后直接调用其bar方法,最后用show显示图像。我解释一下bar中的两个参数:

·        left:柱形的左边缘的位置,如果我们指定1那么当前柱形的左边缘的x值就是1.0了

·        height:这是柱形的高度,也就是Y轴的值了

left,height除了可以使用单独的值(此时是一个柱形),也可以使用元组来替换(此时代表多个矩形)。例如,下面的例子:

参考代码:

>>> import matplotlib.pyplot as plt

>>> plt.bar(left = (0,1),height = (1,0.5))

<Container object of 2 artists>

>>> plt.show()

继续操作:

参考代码:

>>> import matplotlib.pyplot as plt

>>> plt.bar(left = (0,1,2,3,4,5,6),height = (1,0.5,0.7,0.2,0.9,1.6,0.3))

<Container object of 7 artists>

>>> plt.show()

细节注意:上面的默认宽度为0.8,当然自己可以设置宽度的,用属性width进行设置。

 

进一步操作,请看如下需求:

实际情况需要标明X轴与Y轴的标题,怎样操作?比如x轴是性别,y轴是人数。实现也很简单,看代码:

参考代码:

>>> import matplotlib.pyplot as plt

>>> plt.xlabel(u'性别')

<matplotlib.text.Text object at 0x0000000008ABAB70>

>>> plt.ylabel(u'人数')

<matplotlib.text.Text object at 0x000000000B60C898>

>>> plt.bar(left = (0,1),height = (1,0.5),width = 0.35)

<Container object of 2 artists>

>>> plt.show()

注意这里的中文一定要用u(3.0以上好像不用,我用的2.7),因为matplotlib只支持unicode。接下来,让我们在x轴上的每个bar进行说明。比如第一个是“男”,第二个是“女”。

参考代码:

>>> import matplotlib.pyplot as plt

>>> plt.xlabel(u'gender')

<matplotlib.text.Text object at 0x000000000AF42898>

>>> plt.ylabel(u'People Numbers')

<matplotlib.text.Text object at 0x000000000AE62E48>

>>> plt.xticks((0,1),(u'Man',u'Woman'))

([<matplotlib.axis.XTick object at 0x000000000AF32C50>, <matplotlib.axis.XTick object at 0x0000000008951CF8>], <a list of 2 Text xticklabel objects>)

>>> plt.bar(left = (0,1),height = (1,0.5),width = 0.35)

<Container object of 2 artists>

>>> plt.show()

plt.xticks的用法和我们前面说到的left,height的用法差不多。如果你有几个bar,那么就是几维的元组。第一个是文字的位置,第二个是具体的文字说明。不过这里有个问题,很显然我们指定的位置有些“偏移”,最理想的状态应该在每个矩形的中间。你可以更改(0,1)=>((0+0.35)/2 ,(1+0.35)/2 )不过这样比较麻烦。我们可以通过直接指定bar方法里面的align="center"就可以让文字居中了。

参考代码:

>>> import matplotlib.pyplot as plt

>>> plt.xlabel(u'gender')

<matplotlib.text.Text object at 0x0000000008946CC0>

>>> plt.ylabel(u'People Numbers')

<matplotlib.text.Text object at 0x000000000AE356D8>

>>> plt.xticks((0,1),(u'Man',u'Woman'))

([<matplotlib.axis.XTick object at 0x00000000086658D0>, <matplotlib.axis.XTick object at 0x000000000AEEF0F0>], <a list of 2 Text xticklabel objects>)

>>> plt.bar(left = (0,1),height = (1,0.5),width = 0.35,align="center")

<Container object of 2 artists>

>>> plt.show()

接下来,我们还可以给图标加入标题图例

标题:

plt.title(u"性别比例分析")

图例:

rect = plt.bar(left = (0,1),height = (1,0.5),width = 0.35,align="center")
plt.legend((rect,),(u"图例",))

参考代码:

>>> import matplotlib.pyplot as plt

>>> plt.xlabel(u'gender')

<matplotlib.text.Text object at 0x0000000008665A20>

>>> plt.ylabel(u'People Numbers')

<matplotlib.text.Text object at 0x0000000008A298D0>

>>> plt.title(u"Sex ratio analysis")

<matplotlib.text.Text object at 0x000000000B0956D8>

>>> plt.xticks((0,1),(u'Man',u'Woman'))

([<matplotlib.axis.XTick object at 0x0000000008A12FD0>, <matplotlib.axis.XTick object at 0x0000000008AE5438>], <a list of 2 Text xticklabel objects>)

>>> rect = plt.bar(left = (0,1),height = (1,0.5),width = 0.35,align="center")

>>> plt.legend((rect,),(u"Sample",))

<matplotlib.legend.Legend object at 0x000000000AE62CC0>

>>> plt.show()

注意这里的legend方法,里面的参数必须是元组。即使你只有一个图例,不然显示不正确。

 

接下来,我们还可以在每个矩形的上面标注它具体点Y值。这里,我们需要用到一个通用的方法:

参考代码:

import matplotlib.pyplot
as
plt

def autolabel(rects):

    for rect
in
rects:

        height = rect.get_height()

        plt.text(rect.get_x()+rect.get_width()/2.,
1.03*height, '%s' % float(height))

 

plt.xlabel(u'gender')

plt.ylabel(u'People Numbers')

 

plt.title(u"Sex ratio analysis")

plt.xticks((0,1),(u'man',u'woman'))

rect = plt.bar(left = (0,1),height = (1,0.5),width =
0.35,align="center")

 

plt.legend((rect,),(u"Sample",))

autolabel(rect)

 

plt.show()

到这里这个图形已经基本完备了,不过可以看到你一个矩形紧靠这顶部,不是很好看。最好能够空出一段距离出来就好了。这个设置我没有找到具体的属性。不过,我还是通过一个小技巧来实现了。就是bar属性的yerr参数。一旦设置了这个参数,那么对应的矩形上面就会有一个竖着的线,我不知道他是干什么的。不过当我把这个值设置的很小的时候,上面的空白就自动空出来了。如图:

rect =plt.bar(left = (0,1),height = (1,0.5),width =
0.35,align="center",yerr=0.01)

 

参考代码:

import matplotlib.pyplot
as
plt

 

def autolabel(rects):

    for rect
in
rects:

        height = rect.get_height()

        plt.text(rect.get_x()+rect.get_width()/2.,
1.03*height, '%s' % float(height))

 

plt.xlabel(u'gender')

plt.ylabel(u'People Numbers')

 

plt.title(u"Sex ratio analysis")

plt.xticks((0,1),(u'man',u'woman'))

rect = plt.bar(left = (0,1),height = (1,0.5),width =
0.35,align="center",yerr=0.01)

 

plt.legend((rect,),(u"Sample",))

autolabel(rect)

 

plt.show()

参考网址:http://www.cnblogs.com/qianlifeng/archive/2012/02/13/2350086.html

抱歉!评论已关闭.