FFC
FFC

Reputation: 355

Panning on pyplot imshow

I would like to know how to scroll (up/down, left/right) while keeping a viewport size constant using pyplot imshow.

My idea is to take a large numpy array (image) but I only want to show a small part of it at a time. It would be perfect if i could add some panning hand cursor in order to push up/down/right/left my viewport.

Any tips?

Upvotes: 1

Views: 1940

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339490

To turn my comments into an answer:

Matplotlib provides the necessary tools to pan the plot by default. This is detailed in the Interactive navigation article.

enter image description here The Pan/Zoom button
This button has two modes: pan and zoom. Click the toolbar button to activate panning and zooming, then put your mouse somewhere over an axes. Press the left mouse button and hold it to pan the figure, dragging it to a new position. When you release it, the data under the point where you pressed will be moved to the point where you released. If you press ‘x’ or ‘y’ while panning the motion will be constrained to the x or y axis, respectively. [...] You can use the modifier keys ‘x’, ‘y’ or ‘CONTROL’ to constrain the zoom to the x axis, the y axis, or aspect ratio preserve, respectively.

enter image description here

Limiting the axes is possible e.g. via ax.set_xlim(xmin,xmax) and ax.set_ylim(ymin,ymax) or via ax.axis([xmin,xmax,ymin,ymax]).

import numpy as np
import matplotlib.pyplot as plt

im = np.random.rand(600,800)

plt.imshow(im)
plt.gca().axis([200,300,150,250])

plt.show()

Upvotes: 1

Related Questions