2016-04-14 59 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)。 –