How to add lines to contour plot in python `matplotlib`?
<p>I have the following function to illustrate some contour lines :</p>

<pre><code>"""
Illustrate simple contour plotting, contours on an image with
a colorbar for the contours, and labelled contours.

See also contour_image.py.
"""
import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'

X = np.arange(-1.2, 1.2, 0.005)
Y = np.arange(-1.2, 1.2, 0.005)
X, Y = np.meshgrid(X, Y)
Z = (np.ones([np.shape(X)[0],np.shape(X)[1]])-X)**2+100*(Y-(X)**2)**2


# Create a simple contour plot with labels using default colors.  The
# inline argument to clabel will control whether the labels are draw
# over the line segments of the contour, removing the lines beneath
# the label
levels = np.arange(-100.0, 600, 1.0)
plt.figure()
CS = plt.contour(X, 
                 Y, 
                 Z,
                 levels=levels,
                )
plt.clabel(CS, 
           np.array(filter(lambda lev: lev &lt;5.0, levels)),
           inline=0.5, 
           fontsize=10,
           fmt='%1.1f'
          )

plt.hold(True)


plt.plot(np.arange(-1.0, 1.0, 0.005),
        np.arange(-1.0, 1.0, 0.005),
        np.ones(len(np.arange(-1.0, 1.0, 0.005)))*100, '-k')

plt.title('Contour Lines and Constraint of Rosenbrock Optimiztion Problem')
plt.show()
</code></pre>

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

<p>The contour plot looks great if you comment out the lines....:</p>

<pre><code># plt.hold(True)


# plt.plot(np.arange(-1.0, 1.0, 0.005),
#         np.arange(-1.0, 1.0, 0.005),
#         np.ones(len(np.arange(-1.0, 1.0, 0.005)))*100, '-k')
</code></pre>

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

<p>...but I cannot get the lines to show up overlayed on the plot like I need them.  I just simply need them to be overlayed on top of the contour plot. What is the best way to do this?  </p>

<p>I know it is <a href="http://stackoverflow.com/questions/28458192/r-add-a-line-to-contour-plot">possible in R</a>, but how to do this in <code>Python</code> using <code>matplotlib</code>?</p>

<p><code>plt.plot</code> draws a two-dimensional line from a sequence of x- and y-coordinates. There's no z-coordinate associated with each point, so there's no need to pass in a third array argument. At the moment <code>plt.plot</code> is interpreting those arrays as coordinates for two separate lines, and is doing the equivalent of:</p>

<pre><code>plt.plot(np.arange(-1.0, 1.0, 0.005), np.arange(-1.0, 1.0, 0.005))
plt.plot(np.ones(len(np.arange(-1.0, 1.0, 0.005)))*100, '-k')
</code></pre>

<p>Since the second line contains x and y coordinates of up to 100, the axes will be automatically rescaled so that the contour plot is no longer legible.</p>

<p>I think you might be thinking of the <code>zorder=</code> argument (which should just be a scalar rather than an array). It's not necessary in this case - since you're plotting the line after the contours it should have a higher <code>zorder</code> than the contour lines by default. You can just get rid of the third array argument to <code>plt.plot</code></p>

<p>Also, since you're drawing a straight line with only two points, you only need to pass the start and end coordinates:</p>

<pre><code>plt.plot([-1, 1], [-1, 1], '-k')
</code></pre>

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

python matplotlib plot