Skip to content

Commit a04e136

Browse files
committed
DOC: Improve Image Slices Viewer example
1 parent b9c5152 commit a04e136

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

examples/event_handling/image_slices_viewer.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,43 @@
1212
1313
You can copy and paste individual parts, or download the entire example
1414
using the link at the bottom of the page.
15+
16+
The example illustrates how to connect a function to the scroll wheel event.
1517
"""
1618

1719
import numpy as np
1820
import matplotlib.pyplot as plt
1921

2022

21-
# Fixing random state for reproducibility
22-
np.random.seed(19680801)
23-
24-
2523
class IndexTracker:
2624
def __init__(self, ax, X):
27-
self.ax = ax
28-
ax.set_title('use scroll wheel to navigate images')
29-
25+
self.index = 0
3026
self.X = X
31-
rows, cols, self.slices = X.shape
32-
self.ind = self.slices//2
33-
34-
self.im = ax.imshow(self.X[:, :, self.ind])
27+
self.ax = ax
28+
self.im = ax.imshow(self.X[:, :, self.index])
3529
self.update()
3630

3731
def on_scroll(self, event):
38-
print("%s %s" % (event.button, event.step))
39-
if event.button == 'up':
40-
self.ind = (self.ind + 1) % self.slices
41-
else:
42-
self.ind = (self.ind - 1) % self.slices
32+
print(event.button, event.step)
33+
increment = 1 if event.button == 'up' else -1
34+
max_index = self.X.shape[-1] - 1
35+
self.index = np.clip(self.index + increment, 0, max_index)
4336
self.update()
4437

4538
def update(self):
46-
self.im.set_data(self.X[:, :, self.ind])
47-
self.ax.set_ylabel('slice %s' % self.ind)
39+
self.im.set_data(self.X[:, :, self.index])
40+
self.ax.set_title(
41+
f'Use scroll wheel to navigate\nimage {self.index}')
4842
self.im.axes.figure.canvas.draw()
4943

5044

51-
fig, ax = plt.subplots(1, 1)
52-
53-
X = np.random.rand(20, 20, 40)
45+
x, y, z = np.ogrid[-10:10:100j, -10:10:100j, 1:10:20j]
46+
X = np.sin(x * y * z) / (x * y * z)
5447

48+
fig, ax = plt.subplots()
49+
# create an IndexTracker and make sure it lives during the whole
50+
# lifetime of the figure by assigning it to a variable
5551
tracker = IndexTracker(ax, X)
5652

57-
5853
fig.canvas.mpl_connect('scroll_event', tracker.on_scroll)
5954
plt.show()

lib/matplotlib/backend_bases.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2440,6 +2440,14 @@ def func(event: Event) -> Any
24402440
be set to the mouse location in data coordinates. See `.KeyEvent`
24412441
and `.MouseEvent` for more info.
24422442
2443+
.. note::
2444+
2445+
If func is a method, this only stores a weak reference to the
2446+
method. Thus, the figure does not influence the lifetime of
2447+
the associated object. Usually, you want to make sure that the
2448+
object is kept alive throughout the lifetime of the figure by
2449+
holding a reference to it.
2450+
24432451
Returns
24442452
-------
24452453
cid

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy