Windows10+Python3下安裝NumPy+SciPy+Matplotlib


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 import array
  2 from numpy.random import normal
  3 from matplotlib import pyplot
  4 
  5 def genData():
  6     heights = []
  7     weights = []
  8     grades = []
  9     N = 10000
 10 
 11     for i in range(N):
 12         while True:
 13             # 身高服從均值172,標准差為6的正態分布
 14             height = normal(172, 6)
 15             if 0 < height: break
 16         while True:
 17             # 體重由身高作為自變量的線性回歸模型產生,誤差服從標准正態分布
 18             weight = (height - 80) * 0.7 + normal(0, 1)
 19             if 0 < weight: break
 20         while True:
 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     return array(heights), array(weights), array(grades)
 31 
 32 
 33 # 繪制柱狀圖
 34 def drawBar(grades):
 35     xticks = ['A', 'B', 'C', 'D', 'E']
 36     gradeGroup = {}
 37     # 對每一類成績進行頻數統計
 38     for grade in grades:
 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 def drawPie(grades):
 63     labels = ['A', 'B', 'C', 'D', 'E']
 64     gradeGroup = {}
 65     for grade in grades:
 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 def drawHist(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 def drawCumulativeHist(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 def drawScatter(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 def drawBox(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)

 

成功!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM