title: Only plot part of a 3d figure using matplotlib
body: <p>I got a problem when I was plotting a 3d figure using matplotlib of python. Using the following python function, I got this figure:</p>
  
  <p><a href="https://i.stack.imgur.com/twzEy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/twzEy.png" alt="figure1"></a></p>
  
  <p>Here <code>X</code>, <code>Y</code> are meshed grids and <code>Z</code> and <code>Z_</code> are functions of <code>X</code> and <code>Y</code>. <code>C</code> stands for surface color.</p>
  
  <pre><code>import numpy as np
  from mpl_toolkits.mplot3d import Axes3D
  from matplotlib import cm
  import matplotlib.pyplot as plt
  
  def plot(X, Y, Z, Z_, C):
     fig = plt.figure()
     ax = fig.gca(projection='3d')
     surf = ax.plot_surface(
             X, Y, Z, rstride=1, cstride=1,
             facecolors=cm.jet(C),
             linewidth=0, antialiased=False, shade=False)
     surf_ = ax.plot_surface(
             X, Y, Z_, rstride=1, cstride=1,
             facecolors=cm.jet(C),
            linewidth=0, antialiased=False, shade=False)                    
     ax.view_init(elev=7,azim=45)
     plt.show()
  </code></pre>
  
  <p>But now I want to cut this figure horizontally and only the part whose z is between -1 and 2 remain.</p>
  
  <p>What I want, plotted with gnuplot, is this:</p>
  
  <p><a href="https://i.stack.imgur.com/O1dAY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/O1dAY.png" alt="figure 2"></a></p>
  
  <p>I have tried <code>ax.set_zlim3d</code> and <code>ax.set_zlim</code>, but neither of them give me the desired figure. Does anybody know how to do it using python?</p>