From 6249be7d747da489b1162628027990aaae39e6de Mon Sep 17 00:00:00 2001 From: richardsheridan Date: Sat, 19 Jun 2021 18:17:12 -0400 Subject: [PATCH 1/5] add interactive colorbar example to gallery --- .../colormap_interactive_adjustment.py | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 examples/userdemo/colormap_interactive_adjustment.py diff --git a/examples/userdemo/colormap_interactive_adjustment.py b/examples/userdemo/colormap_interactive_adjustment.py new file mode 100644 index 000000000000..e903e45055bb --- /dev/null +++ b/examples/userdemo/colormap_interactive_adjustment.py @@ -0,0 +1,52 @@ +""" +======================================== +Interactive Adjustment of Colormap Range +======================================== + +Demonstration of using colorbar and picker functionality to make an +interactively adjustable colorbar widget. +""" + +import numpy as np +import matplotlib.pyplot as plt +from matplotlib.backend_bases import MouseButton + + +def pick_fn(event): + adjust_colorbar(event.mouseevent) + + +def motion_fn(mouseevent): + if mouseevent.inaxes is colorbar.ax.axes: + adjust_colorbar(mouseevent) + + +def adjust_colorbar(mouseevent): + if mouseevent.button == MouseButton.LEFT: + colorbar.norm.vmax = max(mouseevent.ydata, colorbar.norm.vmin) + elif mouseevent.button == MouseButton.RIGHT: + colorbar.norm.vmin = min(mouseevent.ydata, colorbar.norm.vmax) + else: + # discard all others + return + + canvas.draw_idle() + + +fig, ax = plt.subplots() +canvas = fig.canvas +arr = np.random.random((100, 100)) +axesimage = plt.imshow(arr) +colorbar = plt.colorbar(axesimage, ax=ax, use_gridspec=True) + +# helps you see what value you are about to set the range to +colorbar.ax.set_navigate(True) + +# React to all motion with left or right mouse buttons held +canvas.mpl_connect("motion_notify_event", motion_fn) + +# React only to left and right clicks +colorbar.ax.axes.set_picker(True) +canvas.mpl_connect("pick_event", pick_fn) + +plt.show() From 19b9523e4fb6b21bd33ae221b878a9452192ce5f Mon Sep 17 00:00:00 2001 From: richardsheridan Date: Thu, 24 Jun 2021 18:20:22 -0400 Subject: [PATCH 2/5] respond to code reviews, use better data source --- .../colormap_interactive_adjustment.py | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/examples/userdemo/colormap_interactive_adjustment.py b/examples/userdemo/colormap_interactive_adjustment.py index e903e45055bb..fda7a5e98cd0 100644 --- a/examples/userdemo/colormap_interactive_adjustment.py +++ b/examples/userdemo/colormap_interactive_adjustment.py @@ -3,8 +3,12 @@ Interactive Adjustment of Colormap Range ======================================== -Demonstration of using colorbar and picker functionality to make an +Demonstration of using colorbar, picker, and event functionality to make an interactively adjustable colorbar widget. + +Left clicks and drags inside the colorbar axes adjust the high range of the +color scheme. Likewise, right clicks and drags adjust the low range. The +connected AxesImage immediately updates to reflect the change. """ import numpy as np @@ -12,12 +16,12 @@ from matplotlib.backend_bases import MouseButton -def pick_fn(event): +def on_pick(event): adjust_colorbar(event.mouseevent) -def motion_fn(mouseevent): - if mouseevent.inaxes is colorbar.ax.axes: +def on_move(mouseevent): + if mouseevent.inaxes is colorbar.ax: adjust_colorbar(mouseevent) @@ -35,18 +39,29 @@ def adjust_colorbar(mouseevent): fig, ax = plt.subplots() canvas = fig.canvas -arr = np.random.random((100, 100)) -axesimage = plt.imshow(arr) + +delta = 0.1 +x = np.arange(-3.0, 4.001, delta) +y = np.arange(-4.0, 3.001, delta) +X, Y = np.meshgrid(x, y) +Z1 = np.exp(-X**2 - Y**2) +Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2) +Z = (0.9*Z1 - 0.5*Z2) * 2 + +cmap = plt.get_cmap('viridis').with_extremes(over='xkcd:orange', under='xkcd:dark red') +axesimage = plt.imshow(Z, cmap=cmap) colorbar = plt.colorbar(axesimage, ax=ax, use_gridspec=True) -# helps you see what value you are about to set the range to +# `set_navigate` helps you see what value you are about to set the range +# to, and enables zoom and pan in the colorbar which can be helpful for +# narrow or wide data ranges colorbar.ax.set_navigate(True) # React to all motion with left or right mouse buttons held -canvas.mpl_connect("motion_notify_event", motion_fn) +canvas.mpl_connect("motion_notify_event", on_move) # React only to left and right clicks -colorbar.ax.axes.set_picker(True) -canvas.mpl_connect("pick_event", pick_fn) +colorbar.ax.set_picker(True) +canvas.mpl_connect("pick_event", on_pick) plt.show() From ba252f9a71eff7fb978d86d0a2e8a3de103bece3 Mon Sep 17 00:00:00 2001 From: richardsheridan Date: Thu, 8 Jul 2021 16:37:34 -0400 Subject: [PATCH 3/5] flake8 --- examples/userdemo/colormap_interactive_adjustment.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/userdemo/colormap_interactive_adjustment.py b/examples/userdemo/colormap_interactive_adjustment.py index fda7a5e98cd0..ba42812f6c0d 100644 --- a/examples/userdemo/colormap_interactive_adjustment.py +++ b/examples/userdemo/colormap_interactive_adjustment.py @@ -48,7 +48,8 @@ def adjust_colorbar(mouseevent): Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2) Z = (0.9*Z1 - 0.5*Z2) * 2 -cmap = plt.get_cmap('viridis').with_extremes(over='xkcd:orange', under='xkcd:dark red') +cmap = plt.get_cmap('viridis').with_extremes( + over='xkcd:orange', under='xkcd:dark red') axesimage = plt.imshow(Z, cmap=cmap) colorbar = plt.colorbar(axesimage, ax=ax, use_gridspec=True) From ae8e76f1d6bf5079562d3ea252138c51b3388f1c Mon Sep 17 00:00:00 2001 From: richardsheridan Date: Thu, 8 Jul 2021 17:11:07 -0400 Subject: [PATCH 4/5] Initialize norm and comment on how the axes are synced --- examples/userdemo/colormap_interactive_adjustment.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/examples/userdemo/colormap_interactive_adjustment.py b/examples/userdemo/colormap_interactive_adjustment.py index ba42812f6c0d..676ee96939cd 100644 --- a/examples/userdemo/colormap_interactive_adjustment.py +++ b/examples/userdemo/colormap_interactive_adjustment.py @@ -53,6 +53,12 @@ def adjust_colorbar(mouseevent): axesimage = plt.imshow(Z, cmap=cmap) colorbar = plt.colorbar(axesimage, ax=ax, use_gridspec=True) +# Note that axesimage and colorbar share a Normalize object +# so they will stay in sync +assert colorbar.norm is axesimage.norm +colorbar.norm.vmax = 1.5 +axesimage.norm.vmin = -0.75 + # `set_navigate` helps you see what value you are about to set the range # to, and enables zoom and pan in the colorbar which can be helpful for # narrow or wide data ranges From adff2caa7ac3abaafd511478b25ec342ae041a2d Mon Sep 17 00:00:00 2001 From: richardsheridan Date: Thu, 8 Jul 2021 17:15:43 -0400 Subject: [PATCH 5/5] notebook cell layout --- .../userdemo/colormap_interactive_adjustment.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/examples/userdemo/colormap_interactive_adjustment.py b/examples/userdemo/colormap_interactive_adjustment.py index 676ee96939cd..bb392daf8464 100644 --- a/examples/userdemo/colormap_interactive_adjustment.py +++ b/examples/userdemo/colormap_interactive_adjustment.py @@ -15,6 +15,9 @@ import matplotlib.pyplot as plt from matplotlib.backend_bases import MouseButton +############################################################################### +# Callback definitions + def on_pick(event): adjust_colorbar(event.mouseevent) @@ -37,6 +40,9 @@ def adjust_colorbar(mouseevent): canvas.draw_idle() +############################################################################### +# Generate figure with Axesimage and Colorbar + fig, ax = plt.subplots() canvas = fig.canvas @@ -53,12 +59,17 @@ def adjust_colorbar(mouseevent): axesimage = plt.imshow(Z, cmap=cmap) colorbar = plt.colorbar(axesimage, ax=ax, use_gridspec=True) +############################################################################### # Note that axesimage and colorbar share a Normalize object # so they will stay in sync + assert colorbar.norm is axesimage.norm colorbar.norm.vmax = 1.5 axesimage.norm.vmin = -0.75 +############################################################################### +# Hook Colorbar up to canvas events + # `set_navigate` helps you see what value you are about to set the range # to, and enables zoom and pan in the colorbar which can be helpful for # narrow or wide data ranges @@ -71,4 +82,9 @@ def adjust_colorbar(mouseevent): colorbar.ax.set_picker(True) canvas.mpl_connect("pick_event", on_pick) +############################################################################### +# Display +# +# The colormap will now respond to left and right clicks in the Colorbar axes + plt.show()