Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf77fa2

Browse files
authored
Merge pull request #21268 from greglucas/manual-backport-of-pr-21236-on-v3.5.x
Backport PR #21236: DOC: Update interactive colormap example
2 parents 1198a28 + b33969b commit bf77fa2

File tree

1 file changed

+18
-77
lines changed

1 file changed

+18
-77
lines changed

examples/userdemo/colormap_interactive_adjustment.py

Lines changed: 18 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,88 +3,29 @@
33
Interactive Adjustment of Colormap Range
44
========================================
55
6-
Demonstration of using colorbar, picker, and event functionality to make an
7-
interactively adjustable colorbar widget.
8-
9-
Left clicks and drags inside the colorbar axes adjust the high range of the
10-
color scheme. Likewise, right clicks and drags adjust the low range. The
11-
connected AxesImage immediately updates to reflect the change.
6+
Demonstration of how a colorbar can be used to interactively adjust the
7+
range of colormapping on an image. To use the interactive feature, you must
8+
be in either zoom mode (magnifying glass toolbar button) or
9+
pan mode (4-way arrow toolbar button) and click inside the colorbar.
10+
11+
When zooming, the bounding box of the zoom region defines the new vmin and
12+
vmax of the norm. Zooming using the right mouse button will expand the
13+
vmin and vmax proportionally to the selected region, in the same manner that
14+
one can zoom out on an axis. When panning, the vmin and vmax of the norm are
15+
both shifted according to the direction of movement. The
16+
Home/Back/Forward buttons can also be used to get back to a previous state.
1217
"""
13-
14-
import numpy as np
1518
import matplotlib.pyplot as plt
16-
from matplotlib.backend_bases import MouseButton
17-
18-
###############################################################################
19-
# Callback definitions
20-
21-
22-
def on_pick(event):
23-
adjust_colorbar(event.mouseevent)
24-
25-
26-
def on_move(mouseevent):
27-
if mouseevent.inaxes is colorbar.ax:
28-
adjust_colorbar(mouseevent)
29-
30-
31-
def adjust_colorbar(mouseevent):
32-
if mouseevent.button == MouseButton.LEFT:
33-
colorbar.norm.vmax = max(mouseevent.ydata, colorbar.norm.vmin)
34-
elif mouseevent.button == MouseButton.RIGHT:
35-
colorbar.norm.vmin = min(mouseevent.ydata, colorbar.norm.vmax)
36-
else:
37-
# discard all others
38-
return
39-
40-
canvas.draw_idle()
41-
19+
import numpy as np
4220

43-
###############################################################################
44-
# Generate figure with Axesimage and Colorbar
21+
t = np.linspace(0, 2 * np.pi, 1024)
22+
data2d = np.sin(t)[:, np.newaxis] * np.cos(t)[np.newaxis, :]
4523

4624
fig, ax = plt.subplots()
47-
canvas = fig.canvas
48-
49-
delta = 0.1
50-
x = np.arange(-3.0, 4.001, delta)
51-
y = np.arange(-4.0, 3.001, delta)
52-
X, Y = np.meshgrid(x, y)
53-
Z1 = np.exp(-X**2 - Y**2)
54-
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
55-
Z = (0.9*Z1 - 0.5*Z2) * 2
56-
57-
cmap = plt.colormaps['viridis'].with_extremes(
58-
over='xkcd:orange', under='xkcd:dark red')
59-
axesimage = plt.imshow(Z, cmap=cmap)
60-
colorbar = plt.colorbar(axesimage, ax=ax, use_gridspec=True)
61-
62-
###############################################################################
63-
# Note that axesimage and colorbar share a Normalize object
64-
# so they will stay in sync
65-
66-
assert colorbar.norm is axesimage.norm
67-
colorbar.norm.vmax = 1.5
68-
axesimage.norm.vmin = -0.75
69-
70-
###############################################################################
71-
# Hook Colorbar up to canvas events
72-
73-
# `set_navigate` helps you see what value you are about to set the range
74-
# to, and enables zoom and pan in the colorbar which can be helpful for
75-
# narrow or wide data ranges
76-
colorbar.ax.set_navigate(True)
77-
78-
# React to all motion with left or right mouse buttons held
79-
canvas.mpl_connect("motion_notify_event", on_move)
80-
81-
# React only to left and right clicks
82-
colorbar.ax.set_picker(True)
83-
canvas.mpl_connect("pick_event", on_pick)
25+
im = ax.imshow(data2d)
26+
ax.set_title('Pan on the colorbar to shift the color mapping\n'
27+
'Zoom on the colorbar to scale the color mapping')
8428

85-
###############################################################################
86-
# Display
87-
#
88-
# The colormap will now respond to left and right clicks in the Colorbar axes
29+
fig.colorbar(im, ax=ax, label='Interactive colorbar')
8930

9031
plt.show()

0 commit comments

Comments
 (0)