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

Skip to content

Commit 19b9523

Browse files
respond to code reviews, use better data source
1 parent 6249be7 commit 19b9523

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

examples/userdemo/colormap_interactive_adjustment.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@
33
Interactive Adjustment of Colormap Range
44
========================================
55
6-
Demonstration of using colorbar and picker functionality to make an
6+
Demonstration of using colorbar, picker, and event functionality to make an
77
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.
812
"""
913

1014
import numpy as np
1115
import matplotlib.pyplot as plt
1216
from matplotlib.backend_bases import MouseButton
1317

1418

15-
def pick_fn(event):
19+
def on_pick(event):
1620
adjust_colorbar(event.mouseevent)
1721

1822

19-
def motion_fn(mouseevent):
20-
if mouseevent.inaxes is colorbar.ax.axes:
23+
def on_move(mouseevent):
24+
if mouseevent.inaxes is colorbar.ax:
2125
adjust_colorbar(mouseevent)
2226

2327

@@ -35,18 +39,29 @@ def adjust_colorbar(mouseevent):
3539

3640
fig, ax = plt.subplots()
3741
canvas = fig.canvas
38-
arr = np.random.random((100, 100))
39-
axesimage = plt.imshow(arr)
42+
43+
delta = 0.1
44+
x = np.arange(-3.0, 4.001, delta)
45+
y = np.arange(-4.0, 3.001, delta)
46+
X, Y = np.meshgrid(x, y)
47+
Z1 = np.exp(-X**2 - Y**2)
48+
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
49+
Z = (0.9*Z1 - 0.5*Z2) * 2
50+
51+
cmap = plt.get_cmap('viridis').with_extremes(over='xkcd:orange', under='xkcd:dark red')
52+
axesimage = plt.imshow(Z, cmap=cmap)
4053
colorbar = plt.colorbar(axesimage, ax=ax, use_gridspec=True)
4154

42-
# helps you see what value you are about to set the range to
55+
# `set_navigate` helps you see what value you are about to set the range
56+
# to, and enables zoom and pan in the colorbar which can be helpful for
57+
# narrow or wide data ranges
4358
colorbar.ax.set_navigate(True)
4459

4560
# React to all motion with left or right mouse buttons held
46-
canvas.mpl_connect("motion_notify_event", motion_fn)
61+
canvas.mpl_connect("motion_notify_event", on_move)
4762

4863
# React only to left and right clicks
49-
colorbar.ax.axes.set_picker(True)
50-
canvas.mpl_connect("pick_event", pick_fn)
64+
colorbar.ax.set_picker(True)
65+
canvas.mpl_connect("pick_event", on_pick)
5166

5267
plt.show()

0 commit comments

Comments
 (0)