title: Matplotlib: Time axes and limits
body: <p>I have a matplotlib plot for which the x data is a Pandas DateTimeIndex.</p>
  
  <p>The user can zoom the x-axis using the scroll wheel.  The limits are recalculated like this:</p>
  
  <pre><code>limits = ax.get_xlim()
  span = limits[1] - limits[0]
  pos = (1 - (limits[1] - evt.xdata) / span)
  if evt.button == 'up':
      limits = ( limits[0] + span / 1.3 / 2 * pos,
                 limits[1] - span / 1.3 / 2 * (1 - pos) )
  elif evt.button == 'down':
      limits = ( limits[0] - span / 1.3 / 2 * pos,
                 limits[1] + span / 1.3 / 2 * (1 - pos) )
  </code></pre>
  
  <p>This works and zooms the x-axis in or out on the point where the scroll event happened.  Now I'm trying to recalculate the y-axis limits to zoom to the data displayed but can't figure out how the x-axis data and the x-limits relate to each other:</p>
  
  <pre><code>lines = ax.get_lines()
  y_lim = np.inf, -np.inf
  for line in lines:
      xd = line.get_xdata()
      yd = line.get_ydata()
      y_displayed = yd[((xd &gt; limits[0]) &amp; (xd &lt; limits[1]))]
      y_lim = min(np.min(y_displayed), y_lim[0]), max(np.max(y_displayed), y_lim[1])
  ax.set_ylim(y_lim)
  </code></pre>
  
  <p>This fails with the message <code>TypeError: Invalid type promotion</code>.</p>
  
  <p>As far as I can make out, the x-data is being stored as <code>numpy.datetime64</code> but matplotlib returns the limits as a float which represents the number of days since <code>0001-01-01 00:00</code> plus 1.  How do I get this into some form I can compare with an array of <code>datetime64</code>?</p>