Search code examples
pythonnumpymatplotlibzooming

How to Zoom with Axes3D in Matplotlib


I am generating a 3D plot using matplotlib. I want to be able to zoom in on areas of interest. Currently, I am able to pan but not zoom. Looking at the mplot3d API, I learned about can_pan():

Return True if this axes supports the pan/zoom button functionality.

3D axes objects do not use the pan/zoom button.

and can_zoom():

Return True if this axes supports the zoom box button functionality.

3D axes objects do not use the zoom box button.

They both return False (I think can_pan returns False because the axes cannot pan AND zoom both but maybe I am reading the API wrong).

Is there a way to enable Zoom? The API indicates it does not use the buttons. Is there some way to enable zoom or set it so can_pan() and can_zoom() return True?

Here is a snippet of the code:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

data = np.genfromtxt('data_file.txt')

fig1 = plt.figure()
ax1 = fig1.gca(projection='3d')
ax1.scatter(data[:,0],data[:,1],data[:,2], c='r', marker='.')
plt.show()

ax1.can_zoom()
>>> False

ax1.can_pan()
>>> False

I am using Python 2.7 on an Ubuntu 14.04 64bit desktop version machine with matplotlib installed from the default repositories (I can look the versions up if that is pertinent).


Solution

  • Actually @tcaswell is correct that this functionality doesn't exist and so it returns false. Have you tried zoom-to-rectangle button on the plot window? That works perfectly. If you haven't yet, then refer to the matplotlib instructions on Interactive Navigation. You can zoom in using two ways:

    1. Clicking on pan/zoom button:

      Press the right mouse button to zoom, dragging it to a new position. The x axis will be zoomed in proportionate to the rightward movement and zoomed out proportionate to the leftward movement.

    2. Clicking on zoom-to-rectangle button:

      Put your mouse somewhere over and axes and press the left mouse button. Drag the mouse while holding the button to a new location and release.