2016-04-14 57 views
1

當我運行我的代碼以生成數據集的相對累積頻率圖時,我的圖在圖上穿過線y=1處的一條直線向下右邊,like this oneMatplotlib累積頻率圖與Python中的額外行

y軸被限制在y=0y=1的範圍內,表示累積頻率的0%至100%,一旦圖形達到y=1,或100%,它繼續y=1直到上限x軸從x=0x=2,類似於this graph

有沒有辦法確保y=1已達到y=1的歷史圖案?我需要我的x軸保持在[0,2]範圍內,而y軸保持在[0,1]範圍內。

這裏是我的Python代碼,我用它來生成我的圖表:

import matplotlib.pyplot as plt 
# ... 
plt.ylabel('Relative Cumulative Frequency') 
plt.xlabel('Normalized Eigenvalues') 
plt.hist(e.real, bins = 50, normed=1, histtype='step', cumulative=True) 
# Limit X and Y ranges 
plt.xlim(0, 2) 
plt.ylim(0, 1) 

謝謝,最大

回答

0

您可以通過創建自己的垃圾桶和setting the last bin to np.Inf做到這一點:

import matplotlib.pyplot as plt 
import numpy as np 
... 
x = np.random.rand(100,1) 

plt.ylabel('Relative Cumulative Frequency') 
plt.xlabel('Normalized Eigenvalues') 

binsCnt = 50 
bins = np.append(np.linspace(x.min(), x.max(), binsCnt), [np.inf]) 
plt.hist(x, bins = bins, normed=1, histtype='step', cumulative=True) 
# Limit X and Y ranges 
plt.xlim(0, 2) 
plt.ylim(0, 1) 
plt.show() 

plot

+0

當我運行你輸入的代碼時,我在這行發現一個錯誤:>>> plt.his t(x,normed = 1,bins = bin,histt​​ype ='step',cumulative = 1)',其結果如下: 'UnboundLocalError:在賦值之前引用的局部變量'ymin'。 –

+0

@MaxSamuels我沒有看到任何局部變量ymin。也許你在某個地方忘了全球?什麼是ymin? – incBrain

+0

哦,對不起,忘了添加屏幕截圖到我的[錯誤信息](http://i.stack.imgur.com/1O6LM.png)。 –