2012-05-21 100 views
9

我創建直方圖累積的柱狀圖具有爲y最後一點= 0

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

得到(還有其他的地塊,現在是無關緊要的)紫線

histogram

爲什麼直方圖再次下降到零?累積函數應該一般不減少。有沒有辦法解決這個問題,無論是錯誤還是功能?

編輯:溶液(黑客):

# 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]) 
+0

這工作! Vielen Dank –

回答

0

這是默認的行爲。可以將其視爲柱狀圖的輪廓作爲條形圖。至於一個快速的解決方法,不是我所知道的。一個解決方案是自己計算直方圖:python histogram one-liner

+0

令人失望,但謝謝。我可以計算直方圖(單行不行,這些都是浮動到不規則範圍的浮游物),但實際上我已經這樣做了,儘管我總是喜歡測試的預煮功能。 – eudoxos

0

如果你不喜歡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