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

Skip to content

Commit 68cbc97

Browse files
committed
DOC: Update interactive colormap example
The colormaps have interactivity with zoom/pan now, so demonstrate what those modes do with the new example. This is instead of using callbacks and mouse events for updates.
1 parent 062b5b2 commit 68cbc97

File tree

1 file changed

+65
-78
lines changed

1 file changed

+65
-78
lines changed

examples/images_contours_and_fields/colormap_interactive_adjustment.py

Lines changed: 65 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,90 +3,77 @@
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.
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 or pan mode and click inside the colorbar.
89
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.
10+
When zooming, the bbox of the zoom region defines the new vmin and vmax of
11+
the norm. Right clicking while in zoom mode will expand the
12+
vmin and vmax proportionally to the selected region, in the same manner that
13+
one can zoom out of an axis. When panning, the vmin and vmax of the norm are
14+
both adjusted according to the direction of movement.
15+
16+
The title on the axes show what happens if a zoom or pan occurs on the
17+
colorbar. All of the colorbars in the figure have interactivity enabled and
18+
only update the image that the colorbar is associated with.
1219
1320
.. redirect-from:: /gallery/userdemo/colormap_interactive_adjustment
1421
"""
15-
16-
import numpy as np
1722
import matplotlib.pyplot as plt
18-
from matplotlib.backend_bases import MouseButton
19-
20-
###############################################################################
21-
# Callback definitions
22-
23-
24-
def on_pick(event):
25-
adjust_colorbar(event.mouseevent)
26-
27-
28-
def on_move(mouseevent):
29-
if mouseevent.inaxes is colorbar.ax:
30-
adjust_colorbar(mouseevent)
31-
23+
import numpy as np
3224

33-
def adjust_colorbar(mouseevent):
34-
if mouseevent.button == MouseButton.LEFT:
35-
colorbar.norm.vmax = max(mouseevent.ydata, colorbar.norm.vmin)
36-
elif mouseevent.button == MouseButton.RIGHT:
37-
colorbar.norm.vmin = min(mouseevent.ydata, colorbar.norm.vmax)
25+
start = 0
26+
stop = 2 * np.pi
27+
N = 1024
28+
t = np.linspace(start, stop, N)
29+
data1d = np.sin(t)
30+
31+
data2d = np.sin(t[:, np.newaxis]) * np.cos(t[np.newaxis, :])
32+
33+
zoom_axes = ["2d", "color zoom in", "color zoom out"]
34+
pan_axes = ["2d", "color pan up", "color pan down"]
35+
36+
fig, ax_dict = plt.subplot_mosaic(
37+
[
38+
zoom_axes,
39+
pan_axes
40+
],
41+
constrained_layout=True,
42+
)
43+
44+
for name in zoom_axes:
45+
ax = ax_dict[name]
46+
im = ax.imshow(
47+
data2d,
48+
extent=[start - (stop - start) / (2 * N),
49+
stop + (stop - start) / (2 * N)] * 2,
50+
)
51+
ax.set_title(name)
52+
fig.colorbar(im, ax=ax, aspect=7)
53+
if name == "color zoom in":
54+
im.set_clim(-0.1, 0.1)
55+
elif name == "color zoom out":
56+
im.set_clim(-10, 10)
3857
else:
39-
# discard all others
40-
return
41-
42-
canvas.draw_idle()
43-
44-
45-
###############################################################################
46-
# Generate figure with Axesimage and Colorbar
47-
48-
fig, ax = plt.subplots()
49-
canvas = fig.canvas
50-
51-
delta = 0.1
52-
x = np.arange(-3.0, 4.001, delta)
53-
y = np.arange(-4.0, 3.001, delta)
54-
X, Y = np.meshgrid(x, y)
55-
Z1 = np.exp(-X**2 - Y**2)
56-
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
57-
Z = (0.9*Z1 - 0.5*Z2) * 2
58-
59-
cmap = plt.colormaps['viridis'].with_extremes(
60-
over='xkcd:orange', under='xkcd:dark red')
61-
axesimage = plt.imshow(Z, cmap=cmap)
62-
colorbar = plt.colorbar(axesimage, ax=ax, use_gridspec=True)
63-
64-
###############################################################################
65-
# Note that axesimage and colorbar share a Normalize object
66-
# so they will stay in sync
67-
68-
assert colorbar.norm is axesimage.norm
69-
colorbar.norm.vmax = 1.5
70-
axesimage.norm.vmin = -0.75
71-
72-
###############################################################################
73-
# Hook Colorbar up to canvas events
74-
75-
# `set_navigate` helps you see what value you are about to set the range
76-
# to, and enables zoom and pan in the colorbar which can be helpful for
77-
# narrow or wide data ranges
78-
colorbar.ax.set_navigate(True)
79-
80-
# React to all motion with left or right mouse buttons held
81-
canvas.mpl_connect("motion_notify_event", on_move)
82-
83-
# React only to left and right clicks
84-
colorbar.ax.set_picker(True)
85-
canvas.mpl_connect("pick_event", on_pick)
86-
87-
###############################################################################
88-
# Display
89-
#
90-
# The colormap will now respond to left and right clicks in the Colorbar axes
58+
im.set_clim(-1, 1)
59+
60+
for name in pan_axes:
61+
ax = ax_dict[name]
62+
if name == "2d":
63+
# Don't make this axes twice
64+
continue
65+
im = ax.imshow(
66+
data2d,
67+
extent=[start - (stop - start) / (2 * N),
68+
stop + (stop - start) / (2 * N)] * 2,
69+
)
70+
ax.set_title(name)
71+
fig.colorbar(im, ax=ax, aspect=7)
72+
if name == "color pan up":
73+
im.set_clim(0, 2)
74+
elif name == "color pan down":
75+
im.set_clim(-2, 0)
76+
else:
77+
im.set_clim(-1, 1)
9178

9279
plt.show()

0 commit comments

Comments
 (0)