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

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轴是人数。

实现也非常easy,看代码:

參考代码:

>>> 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

posted @ 2018-11-09 19:07  ldxsuanfa  阅读(605)  评论(0编辑  收藏  举报