Note
Go to the endto download the full example code.
Scroll event#
In this example a scroll wheel event is used to scroll through 2D slices of3D data.
Note
This example exercises the interactive capabilities of Matplotlib, and thiswill not appear in the static documentation. Please run this code on yourmachine to see the interactivity.
You can copy and paste individual parts, or download the entire exampleusing the link at the bottom of the page.

importmatplotlib.pyplotaspltimportnumpyasnpclassIndexTracker:def__init__(self,ax,X):self.index=0self.X=Xself.ax=axself.im=ax.imshow(self.X[:,:,self.index])self.update()defon_scroll(self,event):print(event.button,event.step)increment=1ifevent.button=='up'else-1max_index=self.X.shape[-1]-1self.index=np.clip(self.index+increment,0,max_index)self.update()defupdate(self):self.im.set_data(self.X[:,:,self.index])self.ax.set_title(f'Use scroll wheel to navigate\nindex{self.index}')self.im.axes.figure.canvas.draw()x,y,z=np.ogrid[-10:10:100j,-10:10:100j,1:10:20j]X=np.sin(x*y*z)/(x*y*z)fig,ax=plt.subplots()# create an IndexTracker and make sure it lives during the whole# lifetime of the figure by assigning it to a variabletracker=IndexTracker(ax,X)fig.canvas.mpl_connect('scroll_event',tracker.on_scroll)plt.show()