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

Skip to content

DOC: Update interactive colormap example #21236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,90 +3,31 @@
Interactive Adjustment of Colormap Range
========================================

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.
Demonstration of how a colorbar can be used to interactively adjust the
range of colormapping on an image. To use the interactive feature, you must
be in either zoom mode (magnifying glass toolbar button) or
pan mode (4-way arrow toolbar button) and click inside the colorbar.

When zooming, the bounding box of the zoom region defines the new vmin and
vmax of the norm. Zooming using the right mouse button will expand the
vmin and vmax proportionally to the selected region, in the same manner that
one can zoom out on an axis. When panning, the vmin and vmax of the norm are
both shifted according to the direction of movement. The
Home/Back/Forward buttons can also be used to get back to a previous state.

.. redirect-from:: /gallery/userdemo/colormap_interactive_adjustment
"""

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backend_bases import MouseButton

###############################################################################
# Callback definitions


def on_pick(event):
adjust_colorbar(event.mouseevent)


def on_move(mouseevent):
if mouseevent.inaxes is colorbar.ax:
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()

import numpy as np

###############################################################################
# Generate figure with Axesimage and Colorbar
t = np.linspace(0, 2 * np.pi, 1024)
data2d = np.sin(t)[:, np.newaxis] * np.cos(t)[np.newaxis, :]

fig, ax = plt.subplots()
canvas = fig.canvas

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.colormaps['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)

###############################################################################
# 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
colorbar.ax.set_navigate(True)

# React to all motion with left or right mouse buttons held
canvas.mpl_connect("motion_notify_event", on_move)

# React only to left and right clicks
colorbar.ax.set_picker(True)
canvas.mpl_connect("pick_event", on_pick)
im = ax.imshow(data2d)
ax.set_title('Pan on the colorbar to shift the color mapping\n'
'Zoom on the colorbar to scale the color mapping')

###############################################################################
# Display
#
# The colormap will now respond to left and right clicks in the Colorbar axes
fig.colorbar(im, ax=ax, label='Interactive colorbar')

plt.show()