您的位置:首页 > 编程语言 > Python开发

Python之matplotlib

2015-04-24 14:32 274 查看
matplotlib是python的一个绘图库,命令与matlab很像。

具体的用法可以参照 http://www.cnblogs.com/qianlifeng/archive/2012/02/13/2350086.html
import matplotlib.pyplot as plt
plt.bar(left=0,height=1)
plt.show()




使用元组可以表示多个矩形

plt.bar(left=(0,2),height=(1,0.4)) #用元组表示多个矩形
plt.show()




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));
rect=plt.bar(left=(0,2),height=(1,0.4) ,width=0.3, align='center') #width用来设置矩形宽度
plt.xlabel(u'性别')
plt.ylabel(u'人数') #中文一定要加u,因为matplotlib只支持unicode.但我的电脑依旧乱码
plt.xticks((0,2),('M','W'))   #前面添加的align='center'属性使矩形居中了,与文字对齐
plt.title('Anaysis of China')
plt.legend((rect,),('legend',))
autolabel(rect)
plt.show()


width=0.3 用来设置矩形的宽度, align='center'使矩形居中

plt.xticks((0,2),('M','W')) 给每个矩形块具体的文字说明

plt.legend((rect,),('legend',))用来添加图例

autolabel(rect)是一个自定义的函数,用来实现每个矩阵块上写上它的值.



plt.bar()是条形图

其他几种常见图比如

x=(1,2,3,4,5,6)
y=(2,2,4,7,9,1)
plt.scatter(x,y)
plt.show()




x=(1,2,3,4,5,6)
y=(2,2,4,7,9,1)
plt.plot(x,y)
plt.show()




复制去Google翻译翻译结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: