|
3 | 3 | Interactive Adjustment of Colormap Range |
4 | 4 | ======================================== |
5 | 5 |
|
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. |
12 | 17 | """ |
13 | | - |
14 | | -import numpy as np |
15 | 18 | 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 |
42 | 20 |
|
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, :] |
45 | 23 |
|
46 | 24 | 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') |
84 | 28 |
|
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') |
89 | 30 |
|
90 | 31 | plt.show() |
0 commit comments