Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyplot Interactive Zooming

I want to display an image that is zoomed in when first shown, but still has the ability to zoom out to the full scale using the interactive "Reset original view" button in the figure toolbar. Cropping is completely unacceptable. Using plt.axis([x0, x1, y0, y1]) does allow panning but the interactive window will not reset to full scale.

Is there a way to trigger the plot to zoom or solve this issue another way?

like image 709
CookieOfFortune Avatar asked Feb 25 '14 16:02

CookieOfFortune


1 Answers

A way to do this is:

fig, ax = plt.subplots(1, 1)
ax.imshow(np.random.rand(20, 20)
fig.canvas.toolbar.push_current()  # save the 'un zoomed' view to stack
ax.set_xlim([5, 10])
ax.set_ylim([5, 10])
fig.canvas.toolbar.push_current()  # save 'zoomed' view to stack

I am not sure how private push_current is considered and as I said in the comments this is being refactored for 1.5 (https://github.com/matplotlib/matplotlib/wiki/Mep22).

See https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/backend_bases.py#L2600 for how pan/zoom are implemented. The reason there isn't a 'zoom_window' command is for static images, you just use set_*lim.

like image 166
tacaswell Avatar answered Sep 22 '22 12:09

tacaswell