Vertical line at the end of a CDF histogram using matplotlib
<p>I'm trying to create a CDF but at the end of the graph, there is a vertical line, shown below:</p>

<p><a href="http://i.stack.imgur.com/OIeSJ.png" rel="nofollow"><img src="http://i.stack.imgur.com/OIeSJ.png" alt="plot"></a></p>

<p>I've read that his is because matplotlib uses the end of the bins to draw the vertical lines, which makes sense, so I added into my code as:</p>

<pre><code>bins = sorted(X) + [np.inf]
</code></pre>

<p>where X is the data set I'm using and set the bin size to this when plotting:</p>

<pre><code>plt.hist(X, bins = bins, cumulative = True, histtype = 'step', color = 'b')
</code></pre>

<p>This does remove the line at the end and produce the desired effect, however when I normalise this graph now it produces an error:</p>

<pre><code>ymin = max(ymin*0.9, minimum) if not input_empty else minimum

UnboundLocalError: local variable 'ymin' referenced before assignment
</code></pre>

<p>Is there anyway to either normalise the data with </p>

<pre><code>bins = sorted(X) + [np.inf]
</code></pre>

<p>in my code or is there another way to remove the line on the graph?</p>

<p>An alternative way to plot a CDF would be as follows (in my example, <code>X</code> is a bunch of samples drawn from the unit normal):</p>

<pre><code>import numpy as np, matplotlib.pyplot as plt

X = np.random.randn(10000)
n = np.arange(1,len(X)+1) / np.float(len(X))
Xs = np.sort(X)
plt.plot(Xs,n) #or use plt.step(Xs,n)
</code></pre>

<p><a href="http://i.stack.imgur.com/Zr9pW.png" rel="nofollow"><img src="http://i.stack.imgur.com/Zr9pW.png" alt="enter image description here"></a></p>

python pandas matplotlib