如何在python中的直方图下获取该区域

时间:2014-10-16 08:09:09

标签: python matplotlib histogram area

我一直在绘制直方图。我的代码如下所示:

x0=-15000
x1=15000
b=np.arange(x0,x1,(x1-x0)/250.)

plt.plot(b[0:-1], plt.hist(non_zeros(-e[0]), bins=b, normed=1, visible=0)[0], color = "k", label=r'$\gamma$ = 1.0')

我为直方图设定了标准,因此曲线下的面积等于1. e [0]只是我从文档中获取的一些数据。

我现在想要的是仔细检查直方图下的等于1。怎么办呢?

2 个答案:

答案 0 :(得分:2)

您可以这样计算区域:

import numpy
import matplotlib.pyplot as plt

x = numpy.random.randn(1000)

values, bins, _ = plt.hist(x, normed=True)
area = sum(numpy.diff(bins)*values)

答案 1 :(得分:0)

import numpy as np
import matplotlib.pyplot as plt

x0=-15000
x1=15000
b=np.arange(x0,x1,(x1-x0)/250.)
values, bins, patches = plt.hist(b, 20 ,range=[-15000,15000], facecolor='green',normed=0, alpha=0.5)

#simply here you need to have the lenght of your bins to have a probability for under the graph

len_bins= len(bins)-1
#here is the totaly area under your histogram, which is supposed to be 1 as you want = suming up all the values for of all bins
Total_Area= sum(values[0:len_bins])/sum(values)
#so now lets say you want to have half of the area
Half_Area= sum(values[0:len_bins/2])/sum(values)