Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4fdff43

Browse filesBrowse files
add interactive colorbar example to gallery (#20471)
* add interactive colorbar example to gallery
1 parent b59c814 commit 4fdff43
Copy full SHA for 4fdff43

File tree

Expand file treeCollapse file tree

1 file changed

+90
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+90
-0
lines changed
+90Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""
2+
========================================
3+
Interactive Adjustment of Colormap Range
4+
========================================
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.
12+
"""
13+
14+
import numpy as np
15+
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+
42+
43+
###############################################################################
44+
# Generate figure with Axesimage and Colorbar
45+
46+
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.get_cmap('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)
84+
85+
###############################################################################
86+
# Display
87+
#
88+
# The colormap will now respond to left and right clicks in the Colorbar axes
89+
90+
plt.show()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.