python使用matplotlib绘图详解[python高级教程]
matplotlib是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图。而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中。它的文档相当完备,并且Gallery页面中有上百幅缩略图,打开之后都有源程序。因此如果你需要绘制某种类型的图,只需要在这个页面中浏览/复制/粘贴一下,基本上都能搞定。
这篇我们用matplotlib从构造最简单的bar一步一步向复杂的bar前行。什么是最简单的bar,看如下语句你就知道她有多么简单了:
import matplotlib.pyplot as plt执行效果:plt.bar(left = 0,height = 1)
plt.show()

left:柱形的左边缘的位置,如果我们指定1那么当前柱形的左边缘的x值就是1.0了
height:这是柱形的高度,也就是Y轴的值了
import matplotlib.pyplot as pltplt.bar(left = (0,1),height = (1,0.5))
plt.show()

import matplotlib.pyplot as pltplt.bar(left = (0,1),height = (1,0.5),width = 0.35)
plt.show()

import matplotlib.pyplot as pltplt.xlabel(u"性别")
plt.ylabel(u"人数")
plt.bar(left = (0,1),height = (1,0.5),width = 0.35)
plt.show()

import matplotlib.pyplot as pltplt.xlabel(u"性别")
plt.ylabel(u"人数")
plt.xticks((0,1),(u"男",u"女"))
plt.bar(left = (0,1),height = (1,0.5),width = 0.35)
plt.show()

import matplotlib.pyplot as pltplt.xlabel(u"性别")
plt.ylabel(u"人数")
plt.xticks((0,1),(u"男",u"女"))
plt.bar(left = (0,1),height = (1,0.5),width = 0.35,align="center")
plt.show()

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

import matplotlib.pyplot as pltplt.xlabel(u"性别")
plt.ylabel(u"人数")
plt.title(u"性别比例分析")
plt.xticks((0,1),(u"男",u"女"))
rect = plt.bar(left = (0,1),height = (1,0.5),width = 0.35,align="center")
plt.legend((rect,),(u"图例",))
plt.show()

def autolabel(rects):其中plt.text的参数分别是:x坐标,y坐标,要显示的文字。所以,调用代码如下:for rect in rects:
height = rect.get_height()
plt.text(rect.get_x()+rect.get_width()/2., 1.03*height, "%s" % float(height))
import matplotlib.pyplot as pltdef 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"性别")
plt.ylabel(u"人数")
plt.title(u"性别比例分析")
plt.xticks((0,1),(u"男",u"女"))
rect = plt.bar(left = (0,1),height = (1,0.5),width = 0.35,align="center")
plt.legend((rect,),(u"图例",))
autolabel(rect)
plt.show()

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

以上是 python使用matplotlib绘图详解[python高级教程] 的全部内容, 来源链接: utcz.com/z/539995.html