累积直方图的最后一个点位于y=0。

16

我正在创建直方图

pylab.hist(data,weights,histtype='step',normed=False,bins=150,cumulative=True)

获取(还有其他的图表,现在不相关)紫色线

histogram

为什么直方图在末尾又降到零了?累积函数通常应是非递减的。是否存在解决此问题的方法,无论是漏洞还是功能?

3个回答

1
如果您不喜欢OP提供的简单解决方案,这里有一个过于复杂的解决方案,我们需要手动构建绘图。但是,如果您只能访问直方图计数并且不能使用matplotlib的hist函数,这可能会很有用。
import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(5000)
counts, bins = np.histogram(data, bins=20)
cdf = np.cumsum(counts)/np.sum(counts)

plt.plot(
    np.vstack((bins, np.roll(bins, -1))).T.flatten()[:-2],
    np.vstack((cdf, cdf)).T.flatten()
)
plt.show()

output


0

这是默认行为。可以将其视为直方图的条形图轮廓。至于快速解决方法,我不知道有什么。一个解决方案是自己计算直方图:python histogram one-liner


有点失望,但还是谢谢。我可以计算直方图(一行代码不行,那些是浮点数,需要分成规律间隔的范围),实际上我已经这样做了,尽管我总是更喜欢测试过的预先编写好的函数。 - eudoxos

0

解决方案(hack):

# histtype=step returns a single patch, open polygon
n,bins,patches=pylab.hist(data, weights, histtype='step', cumulative=True)
# just delete the last point
patches[0].set_xy(patches[0].get_xy()[:-1])

这篇回答是由OP Zer0 在CC BY-SA 3.0下发布的,并作为编辑问题累积直方图在y = 0处有最后一个点


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接