Python contour plotting wrong values with plot_surface
Question: I want to plot a surface in Matplotlib consisting of zeros everywhere, except
for a rectangular region centered in (0, 0), with sides (Dx, Dy), consisting
of ones - kind of like a table, if you wil; I can do that using the
`plot_surface` command, no worries there. I also want to plot its projections
in the "x" and "y" directions (as in [this
demo](http://matplotlib.org/examples/mplot3d/contour3d_demo3.html)) and that's
when the results become weird: Python seems to be interpolating my amplitude
values (which, again, should be either zero or one) for the contour plots and
showing some lines with values that do not correspond to my data points.

This is what I'm doing:

    
    
    import numpy
    from matplotlib import pylab
    from mpl_toolkits.mplot3d import axes3d
    
    Dx = 1.    # Define the sides of the rectangle
    Dy = 2.
    
    x_2D = numpy.linspace(-Dx, Dx, 100)     # Create the mesh points
    y_2D = numpy.linspace(-Dy, Dy, 100)
    x_mesh, y_mesh = numpy.meshgrid(x_2D, y_2D)
    
    rect_2D = numpy.zeros(x_mesh.shape)    # All values of "rect_2D" are zero...
    for i in range(x_2D.size):
        for j in range(y_2D.size):
            if numpy.abs(x_mesh[i, j]) <= Dx/2 and numpy.abs(y_mesh[i, j]) <= Dy/2:
                rect_2D[i, j] = 1.         # ... except these ones
    
    fig = pylab.figure(figsize=(9, 7))
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_surface(x_mesh, y_mesh, rect_2D, alpha=0.3)
    
    ax.contour(x_mesh, y_mesh, rect_2D, zdir='x', offset=-1.5, cmap=pylab.cm.brg)
    ax.contour(x_mesh, y_mesh, rect_2D, zdir='y', offset=3, cmap=pylab.cm.brg)
    
    ax.set_xlim(-1.5, 1.5)
    ax.set_ylim(-3, 3)
    ax.set_zlim(0., 1.5)
    
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('z')
    

The resulting figure has a dark green line with amplitude a bit below 0.8 on
both the "x" and "y" projections, which does not exist in my "rect_2D"
variable. Does anyone knows if this is a bug or if there is a mistake in my
code? Any suggestions on how to get rid of it? Thanks in advance!


Answer: Add a `levels = [0],` kwarg to your `ax.contour` call. This specifies where
along the `zdir` axis your contours are computed. See `mpl.axes.Axes.contour`
docstring for more info.

The problem is that without specifying `levels`, contour automatically
computes the locations to plot contours and one of these contours is selected
just past the 'edge of your table', but before your Zdata is 0. At these
points contour interpolates between 0 and 1.

