Matplotlib 累积直方图的最后一点为y=0

Matplotlib 累积直方图的最后一点为y=0,matplotlib,histogram,Matplotlib,Histogram,我正在创建直方图 pylab.hist(data,weights,histtype='step',normed=False,bins=150,cumulative=True) 获得(还有其他的情节,现在不相关)紫线 为什么柱状图在最后又降为零?累积函数一般应为非递减函数。有没有办法解决这个问题,无论是bug还是特性 编辑:解决方案(黑客): # histtype=step returns a single patch, open polygon n,bins,patches=pylab.hi

我正在创建直方图

pylab.hist(data,weights,histtype='step',normed=False,bins=150,cumulative=True)
获得(还有其他的情节,现在不相关)紫线

为什么柱状图在最后又降为零?累积函数一般应为非递减函数。有没有办法解决这个问题,无论是bug还是特性

编辑:解决方案(黑客):

# 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的简单解决方案,这里有一个过于复杂的解决方案,我们用手绘制图。如果您只能访问直方图计数,而不能使用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()

令人失望,但谢谢。我可以计算柱状图(一行不行,那些是浮动,被分为任意间隔的范围),我实际上已经计算过了,尽管我总是更喜欢经过测试的预处理函数。