8000 Merge pull request #24381 from meeseeksmachine/auto-backport-of-pr-24… · matplotlib/matplotlib@3702fd5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3702fd5

Browse files
authored
Merge pull request #24381 from meeseeksmachine/auto-backport-of-pr-24366-on-v3.6.x
Backport PR #24366 on branch v3.6.x (DOC: Improve Image Slices Viewer example)
2 parents a231201 + dc0c750 commit 3702fd5

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,14 @@ def func(event: Event) -> Any
24212421
be set to the mouse location in data coordinates. See `.KeyEvent`
24222422
and `.MouseEvent` for more info.
24232423
2424+
.. note::
2425+
2426+
If func is a method, this only stores a weak reference to the
2427+
method. Thus, the figure does not influence the lifetime of
2428+
the associated object. Usually, you want to make sure that the
2429+
object is kept alive throughout the lifetime of the figure by
2430+
holding a reference to it.
2431+
24242432
Returns
24252433
-------
24262434
cid

0 commit comments

Comments
 (0)
0