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

用matplotlib作图步骤

2016-01-18 22:12 666 查看
装完matplotlib完后立即着手实践。找了一段代码来完成作业图

环境: windows

Python版本: 2.7

Python开发工具:IDLE

(1)打开shell终端窗口,选择File菜单下的New File 选项,新建一个python文件,命名1.py,并保存

(2)此时shell终端就会变成编辑界面,会多出Run菜单,选择运行即可,出现图形好激动,有没有哈。。。

(3)附:我的源码如下:

import numpy as np

import matplotlib.pyplot as plt

N = 5

menMeans = (20, 35, 30, 35, 27)

menStd = (2, 3, 4, 1, 2)

ind = np.arange(N) # the x locations for the groups

width = 0.35 # the width of the bars

fig, ax = plt.subplots()

rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd)

womenMeans = (25, 32, 34, 20, 25)

womenStd = (3, 5, 2, 3, 3)

rects2 = ax.bar(ind+width, womenMeans, width, color='y', yerr=womenStd)

# add some

ax.set_ylabel('Scores')

ax.set_title('Scores by group and gender')

ax.set_xticks(ind+width)

ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5') )

ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )

def autolabel(rects):

# attach some text labels

for rect in rects:

height = rect.get_height()

ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),

ha='center', va='bottom')

autolabel(rects1)

autolabel(rects2)

plt.show()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: