| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 062b5b2 commit adace6d
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,90 +3,31 @@ | |||
| 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 | 18 | .. redirect-from:: /gallery/userdemo/colormap_interactive_adjustment | |
| 14 | 19 | """ | |
| 15 | - | ||
| 16 | - import numpy as np | ||
| 17 | 20 | 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 | - | ||
| 32 | - | ||
| 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) | ||
| 38 | - else: | ||
| 39 | - # discard all others | ||
| 40 | - return | ||
| 41 | - | ||
| 42 | - canvas.draw_idle() | ||
| 43 | - | ||
| 21 | + import numpy as np | ||
| 44 | 22 | ||
| 45 | - ############################################################################### | ||
| 46 | - # Generate figure with Axesimage and Colorbar | ||
| 23 | + t = np.linspace(0, 2 * np.pi, 1024) | ||
| 24 | + data2d = np.sin(t)[:, np.newaxis] * np.cos(t)[np.newaxis, :] | ||
| 47 | 25 | ||
| 48 | 26 | 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) | ||
| 27 | + im = ax.imshow(data2d) | ||
| 28 | + ax.set_title('Pan on the colorbar to shift the norm\n' | ||
| 29 | + 'Zoom on the colorbar to scale the norm') | ||
| 86 | 30 | ||
| 87 | - ############################################################################### | ||
| 88 | - # Display | ||
| 89 | - # | ||
| 90 | - # The colormap will now respond to left and right clicks in the Colorbar axes | ||
| 31 | + fig.colorbar(im, ax=ax, label='Interactive colorbar') | ||
| 91 | 32 | ||
| 92 | 33 | plt.show() | |
| Back | FazBrowse Home | New Git URL |
0 commit comments