Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically setting matplotlib plot to initial zoom level [duplicate]

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 890
CookieOfFortune Avatar asked Nov 19 '22 23:11

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 189
tacaswell Avatar answered Jun 12 '23 07:06

tacaswell