Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add lines to contour plot in python `matplotlib`?

I have the following function to illustrate some contour lines :

"""
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 <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()

enter image description here

The contour plot looks great if you comment out the lines....:

# 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')

enter image description here

...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?

I know it is possible in R, but how to do this in Python using matplotlib?

like image 609
makansij Avatar asked Sep 26 '16 19:09

makansij


1 Answers

plt.plot 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 plt.plot is interpreting those arrays as coordinates for two separate lines, and is doing the equivalent of:

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')

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.

I think you might be thinking of the zorder= 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 zorder than the contour lines by default. You can just get rid of the third array argument to plt.plot

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

plt.plot([-1, 1], [-1, 1], '-k')

enter image description here

like image 190
ali_m Avatar answered Sep 29 '22 10:09

ali_m