-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
add interactive colorbar example to gallery #20471
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
jklymak
merged 5 commits into
matplotlib:master
from
richardsheridan:interactive_colorbar
Jul 21, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6249be7
add interactive colorbar example to gallery
richardsheridan 19b9523
respond to code reviews, use better data source
richardsheridan ba252f9
flake8
richardsheridan ae8e76f
Initialize norm and comment on how the axes are synced
richardsheridan adff2ca
notebook cell layout
richardsheridan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
""" | ||
======================================== | ||
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. | ||
""" | ||
|
||
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() | ||
|
||
|
||
############################################################################### | ||
# Generate figure with Axesimage and Colorbar | ||
|
||
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.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) | ||
|
||
############################################################################### | ||
# 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) | ||
|
||
############################################################################### | ||
# Display | ||
# | ||
# The colormap will now respond to left and right clicks in the Colorbar axes | ||
|
||
plt.show() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.