数组的分布图

时间:2014-03-29 08:56:25

标签: python arrays numpy statistics probability

我有一个numpy数组,其中包含[-10..10]中的浮点值。我想绘制一个值的分布图,就像这样(这里是二项式随机变量):

enter image description here

例如,我希望条形计算每个区间[-10,-9.5],[ - 9.5,-9],......,[9.5,10]中元素的数量。

  

如何使用Python准备这样的分布图?

1 个答案:

答案 0 :(得分:10)

确实是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()

n包含每个bin中的点数和bins自动生成的示例中的截断值。您当然可以使用plt.hist的选项来获取您想要的图表。

在您的情况下,只需将数组替换为x,然后使用bins选项进行截断值,例如:

plt.hist(x, bins = [-10, -9.5, -9])

您还可以简单地将标量n传递给bins,在这种情况下,plt.hist将确定截止值,以显示带有n个分箱的精美图表。