Bas*_*asj 5 python arrays statistics numpy probability
我有一个numpy包含浮点值的数组在[-10..10].我想绘制一个值的分布图,就像这样(这里是二项式随机变量):

例如,我想条形计算每个区间[-10,-9.5],[ - 9.5,-9],......,[9.5,10]中元素的数量.
如何用Python编写这样的分布图?
etn*_*tna 13
确实是matplotlib,更确切地说,你会发现代码的样本对应于你所追求的:http://matplotlib.org/examples/pylab_examples/histogram_demo_extended.html
import numpy as np
import matplotlib.pyplot as plt
mu, sigma = 200, 25
x = mu + sigma*np.random.randn(10000)
n, bins, patches = plt.hist(x)
plt.show()
Run Code Online (Sandbox Code Playgroud)
n包含每个bin中的点数以及bins我的示例中自动生成的截断值.您当然可以使用plt.hist选项来获取您想要的图形.
在您的情况下,只需替换x您的数组,并使用bins截断值的选项,例如:
plt.hist(x, bins = [-10, -9.5, -9])
Run Code Online (Sandbox Code Playgroud)
你也可以简单地传递标量n,bins在这种情况下plt.hist将确定截止值以显示带有分n档的漂亮图形.
| 归档时间: |
|
| 查看次数: |
16696 次 |
| 最近记录: |