8000 DOC: Improve Image Slices Viewer example · matplotlib/matplotlib@fbca556 · GitHub
[go: up one dir, main page]

Skip to content

Commit fbca556

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

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed
Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""
2-
===================
3-
Image Slices Viewer
4-
===================
2+
============
3+
Scroll event
4+
============
55
6-
Scroll through 2D image slices of a 3D array.
6+
In this example a scroll wheel event is used to scroll through 2D slices of
7+
3D data.
78
89
.. note::
910
This example exercises the interactive capabilities of Matplotlib, and this
@@ -18,42 +19,35 @@
1819
import matplotlib.pyplot as plt
1920

2021

21-
# Fixing random state for reproducibility
22-
np.random.seed(19680801)
23-
24-
2522
class IndexTracker:
2623
def __init__(self, ax, X):
27-
self.ax = ax
28-
ax.set_title('use scroll wheel to navigate images')
29-
24+
self.index = 0
3025
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])
26+
self.ax = ax
27+
self.im = ax.imshow(self.X[:, :, self.index])
3528
self.update()
3629

3730
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
31+
print(event.button, event.step)
32+
increment = 1 if event.button == 'up' else -1
33+
max_index = self.X.shape[-1] - 1
34+
self.index = np.clip(self.index + increment, 0, max_index)
4335
self.update()
4436

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

5043

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

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

57-
5852
fig.canvas.mpl_connect('scroll_event', tracker.on_scroll)
5953
plt.show()

lib/matplotlib/backend_bases.py 9D2E

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)
0