Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitb01d5c6

Browse files
committed
DOC: Update interactive colormap example
The colormaps have interactivity with zoom/pan now, so demonstratewhat those modes do with the new example. This is instead of usingcallbacks and mouse events for updates.
1 parent062b5b2 commitb01d5c6

File tree

1 file changed

+42
-80
lines changed

1 file changed

+42
-80
lines changed

‎examples/images_contours_and_fields/colormap_interactive_adjustment.py

Lines changed: 42 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,90 +3,52 @@
33
Interactive Adjustment of Colormap Range
44
========================================
55
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.
6+
Demonstration of how a colorbar can be used to interactively adjust the
7+
range of colormapping on an image. To use the interactive feature, you must
8+
be in either zoom mode (magnifying glass toolbar button) or
9+
pan mode (4-way arrow toolbar button) and click inside the colorbar.
10+
11+
When zooming, the bbox of the zoom region defines the new vmin and vmax of
12+
the norm. Zooming using the right mouse button will expand the
13+
vmin and vmax proportionally to the selected region, in the same manner that
14+
one can zoom out of an axis. When panning, the vmin and vmax of the norm are
15+
both adjusted according to the direction of movement. The
16+
Home/Back/Forward buttons can also be used to get back to a previous state.
17+
18+
The data in the left and right images contain values that are
19+
out of the range of the Normalize object. The zoom and pan functionality
20+
on the colorbar can be used to adjust the range of the underlying norm
21+
and more clearly see what is in each of the left and right images.
1222
1323
.. redirect-from:: /gallery/userdemo/colormap_interactive_adjustment
1424
"""
15-
16-
importnumpyasnp
1725
importmatplotlib.pyplotasplt
18-
frommatplotlib.backend_basesimportMouseButton
19-
20-
###############################################################################
21-
# Callback definitions
22-
23-
24-
defon_pick(event):
25-
adjust_colorbar(event.mouseevent)
26-
27-
28-
defon_move(mouseevent):
29-
ifmouseevent.inaxesiscolorbar.ax:
30-
adjust_colorbar(mouseevent)
31-
32-
33-
defadjust_colorbar(mouseevent):
34-
ifmouseevent.button==MouseButton.LEFT:
35-
colorbar.norm.vmax=max(mouseevent.ydata,colorbar.norm.vmin)
36-
elifmouseevent.button==MouseButton.RIGHT:
37-
colorbar.norm.vmin=min(mouseevent.ydata,colorbar.norm.vmax)
38-
else:
39-
# discard all others
40-
return
41-
42-
canvas.draw_idle()
43-
44-
45-
###############################################################################
46-
# Generate figure with Axesimage and Colorbar
47-
48-
fig,ax=plt.subplots()
49-
canvas=fig.canvas
50-
51-
delta=0.1
52-
x=np.arange(-3.0,4.001,delta)
53-
y=np.arange(-4.0,3.001,delta)
54-
X,Y=np.meshgrid(x,y)
55-
Z1=np.exp(-X**2-Y**2)
56-
Z2=np.exp(-(X-1)**2- (Y-1)**2)
57-
Z= (0.9*Z1-0.5*Z2)*2
58-
59-
cmap=plt.colormaps['viridis'].with_extremes(
60-
over='xkcd:orange',under='xkcd:dark red')
61-
axesimage=plt.imshow(Z,cmap=cmap)
62-
colorbar=plt.colorbar(axesimage,ax=ax,use_gridspec=True)
63-
64-
###############################################################################
65-
# Note that axesimage and colorbar share a Normalize object
66-
# so they will stay in sync
67-
68-
assertcolorbar.normisaxesimage.norm
69-
colorbar.norm.vmax=1.5
70-
axesimage.norm.vmin=-0.75
71-
72-
###############################################################################
73-
# Hook Colorbar up to canvas events
74-
75-
# `set_navigate` helps you see what value you are about to set the range
76-
# to, and enables zoom and pan in the colorbar which can be helpful for
77-
# narrow or wide data ranges
78-
colorbar.ax.set_navigate(True)
79-
80-
# React to all motion with left or right mouse buttons held
81-
canvas.mpl_connect("motion_notify_event",on_move)
82-
83-
# React only to left and right clicks
84-
colorbar.ax.set_picker(True)
85-
canvas.mpl_connect("pick_event",on_pick)
26+
frommatplotlib.colorsimportNormalize
27+
importnumpyasnp
8628

87-
###############################################################################
88-
# Display
89-
#
90-
# The colormap will now respond to left and right clicks in the Colorbar axes
29+
start=0
30+
stop=2*np.pi
31+
N=1024
32+
t=np.linspace(start,stop,N)
33+
data2d=np.sin(t)[:,np.newaxis]*np.cos(t)[np.newaxis, :]
34+
35+
fig,axarr=plt.subplots(ncols=3)
36+
37+
# Create a norm that is shared by all of the axes
38+
norm=Normalize(vmin=-1,vmax=1)
39+
40+
forax,name,offsetinzip(axarr,
41+
['(-2, 0)','(-1, 1)','(0, 2)'],
42+
[-1,0,1]):
43+
im=ax.imshow(
44+
data2d+offset,
45+
extent=[start- (stop-start)/ (2*N),
46+
stop+ (stop-start)/ (2*N)]*2,
47+
norm=norm,
48+
)
49+
ax.set_title(f'Data range:{name}')
50+
51+
fig.colorbar(im,ax=axarr,orientation='horizontal',
52+
label='Interactive colorbar that links to all axes')
9153

9254
plt.show()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp