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

2024-04-26 03:50:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我画了一个柱状图。我的代码如下:

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。怎么能做到?


Tags: 代码plotnpzerosplthistcolorx1
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)
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)

相关问题 更多 >