2017-08-02 128 views
1
Python 3.6.1 :: Anaconda custom (64-bit) 

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib as mtptlb 

print (np.__version__) 
1.12.1 
print (mtptlb.__version__) 
2.0.2 

%matplotlib inline 
a=np.random.uniform(1,100,1000000) 
b=range(1,101) 
plt.hist(a) 

enter image description herePython的numpy的隨機數的概率

爲什麼Y軸顯示? np.random.uniform(1,100,)的值爲1000000,所以不應該在y軸上顯示1000000?

回答

7

默認matplotlib.pyplot.hist使用10箱。所以你所有的100萬價值分配到10箱。對於一個完美的均勻分佈,你會期望在每個bin中有100k事件(100萬除以10)。

你可以改變窗口的數量,即

a=np.random.uniform(1, 100, 1000000) 
plt.hist(a, bins=100) 

enter image description here

這裏它分爲100個箱,並因爲它是均勻分佈的所有垃圾桶都大致在10000

或者只是如果您希望計數爲1 000 000,請使用一個bin:

a=np.random.uniform(1, 100, 1000000) 
plt.hist(a, bins=1) 

enter image description here