Numpy、SciPy、MatplotLib是Python下从事科学计算必不可少的库。我在用其他的方法安装时出现各种问题,发现直接安装.whl包是最快且不报错的方法。
1.下载.whl包
在下面的网站中找需要的.whl文件下载
http://www.lfd.uci.edu/~gohlke/pythonlibs/
要和自己本地安装的版本一致,我选择的whl文件是:
numpy-1.13.0+mkl-cp36-cp36m-win32.whl
scipy-0.19.1-cp36-cp36m-win32.whl
matplotlib-2.0.2-cp36-cp36m-win32.whl
2.开始在命令行安装
>pip3 install c:\(whl文件下载的路径)\numpy-1.13.0+mkl-cp36-cp36m-win32.whl
>pip3 install c:\(whl文件下载的路径)\scipy-0.19.1-cp36-cp36m-win32.whl
>pip3 install c:\(whl文件下载的路径)\matplotlib-2.0.2-cp36-cp36m-win32.whl
如果不出意外,这就都安装好了。
3.开始测试
测试代码来自:http://www.cnblogs.com/jasonfreak/p/5441512.html 感谢作者
1 from numpy importarray2 from numpy.random importnormal3 from matplotlib importpyplot4
5 defgenData():6 heights =[]7 weights =[]8 grades =[]9 N = 10000
10
11 for i inrange(N):12 whileTrue:13 #身高服从均值172,标准差为6的正态分布
14 height = normal(172, 6)15 if 0 < height: break
16 whileTrue:17 #体重由身高作为自变量的线性回归模型产生,误差服从标准正态分布
18 weight = (height - 80) * 0.7 + normal(0, 1)19 if 0 < weight: break
20 whileTrue:21 #分数服从均值为70,标准差为15的正态分布
22 score = normal(70, 15)23 if 0 <= score and score <= 100:24 grade = 'E' if score < 60 else(25 'D' if score < 70 else ('C' if score < 80 else ('B' if score < 90 else 'A')))26 break
27 heights.append(height)28 weights.append(weight)29 grades.append(grade)30 returnarray(heights), array(weights), array(grades)31
32
33 #绘制柱状图
34 defdrawBar(grades):35 xticks = ['A', 'B', 'C', 'D', 'E']36 gradeGroup ={}37 #对每一类成绩进行频数统计
38 for grade ingrades:39 gradeGroup[grade] = gradeGroup.get(grade, 0) + 1
40 #创建柱状图
41 #第一个参数为柱的横坐标
42 #第二个参数为柱的高度
43 #参数align为柱的对齐方式,以第一个参数为参考标准
44 pyplot.bar(range(5), [gradeGroup.get(xtick, 0) for xtick in xticks], align='center')45
46 #设置柱的文字说明
47 #第一个参数为文字说明的横坐标
48 #第二个参数为文字说明的内容
49 pyplot.xticks(range(5), xticks)50
51 #设置横坐标的文字说明
52 pyplot.xlabel('Grade')53 #设置纵坐标的文字说明
54 pyplot.ylabel('Frequency')55 #设置标题
56 pyplot.title('Grades Of Male Students')57 #绘图
58 pyplot.show()59
60
61 #绘制饼形图
62 defdrawPie(grades):63 labels = ['A', 'B', 'C', 'D', 'E']64 gradeGroup ={}65 for grade ingrades:66 gradeGroup[grade] = gradeGroup.get(grade, 0) + 1
67 #创建饼形图
68 #第一个参数为扇形的面积
69 #labels参数为扇形的说明文字
70 #autopct参数为扇形占比的显示格式
71 pyplot.pie([gradeGroup.get(label, 0) for label in labels], labels=labels, autopct='%1.1f%%')72 pyplot.title('Grades Of Male Students')73 pyplot.show()74
75
76 #绘制直方图
77 defdrawHist(heights):78 #创建直方图
79 #第一个参数为待绘制的定量数据,不同于定性数据,这里并没有事先进行频数统计
80 #第二个参数为划分的区间个数
81 pyplot.hist(heights, 100)82 pyplot.xlabel('Heights')83 pyplot.ylabel('Frequency')84 pyplot.title('Heights Of Male Students')85 pyplot.show()86
87
88 #绘制累积曲线
89 defdrawCumulativeHist(heights):90 #创建累积曲线
91 #第一个参数为待绘制的定量数据
92 #第二个参数为划分的区间个数
93 #normed参数为是否无量纲化
94 #histtype参数为'step',绘制阶梯状的曲线
95 #cumulative参数为是否累积
96 pyplot.hist(heights, 20, normed=True, histtype='step', cumulative=True)97 pyplot.xlabel('Heights')98 pyplot.ylabel('Frequency')99 pyplot.title('Heights Of Male Students')100 pyplot.show()101
102
103 #绘制散点图
104 defdrawScatter(heights, weights):105 #创建散点图
106 #第一个参数为点的横坐标
107 #第二个参数为点的纵坐标
108 pyplot.scatter(heights, weights)109 pyplot.xlabel('Heights')110 pyplot.ylabel('Weights')111 pyplot.title('Heights & Weights Of Male Students')112 pyplot.show()113
114
115 #绘制箱形图
116 defdrawBox(heights):117 #创建箱形图
118 #第一个参数为待绘制的定量数据
119 #第二个参数为数据的文字说明
120 pyplot.boxplot([heights], labels=['Heights'])121 pyplot.title('Heights Of Male Students')122 pyplot.show()123
124 data =genData()125 print(data)126 heights =data[0]127 weights = data[1]128 grades = data[2]129 drawBar(grades)130 drawPie(grades)131 drawHist(heights)132 drawCumulativeHist(heights)133 drawScatter(heights, weights)134 drawBox(heights)
运行结果:
drawBar(grades)
drawPie(grades)
drawHist(heights)
drawCumulativeHist(heights)
drawScatter(heights, weights)
drawBox(heights)
成功!