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

Commit68cbc97

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 commit68cbc97

File tree

1 file changed

+65
-78
lines changed

1 file changed

+65
-78
lines changed

‎examples/images_contours_and_fields/colormap_interactive_adjustment.py

Lines changed: 65 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,90 +3,77 @@
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.
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 or pan mode and click inside the colorbar.
89
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.
10+
When zooming, the bbox of the zoom region defines the new vmin and vmax of
11+
the norm. Right clicking while in zoom mode will expand the
12+
vmin and vmax proportionally to the selected region, in the same manner that
13+
one can zoom out of an axis. When panning, the vmin and vmax of the norm are
14+
both adjusted according to the direction of movement.
15+
16+
The title on the axes show what happens if a zoom or pan occurs on the
17+
colorbar. All of the colorbars in the figure have interactivity enabled and
18+
only update the image that the colorbar is associated with.
1219
1320
.. redirect-from:: /gallery/userdemo/colormap_interactive_adjustment
1421
"""
15-
16-
importnumpyasnp
1722
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-
23+
importnumpyasnp
3224

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)
25+
start=0
26+
stop=2*np.pi
27+
N=1024
28+
t=np.linspace(start,stop,N)
29+
data1d=np.sin(t)
30+
31+
data2d=np.sin(t[:,np.newaxis])*np.cos(t[np.newaxis, :])
32+
33+
zoom_axes= ["2d","color zoom in","color zoom out"]
34+
pan_axes= ["2d","color pan up","color pan down"]
35+
36+
fig,ax_dict=plt.subplot_mosaic(
37+
[
38+
zoom_axes,
39+
pan_axes
40+
],
41+
constrained_layout=True,
42+
)
43+
44+
fornameinzoom_axes:
45+
ax=ax_dict[name]
46+
im=ax.imshow(
47+
data2d,
48+
extent=[start- (stop-start)/ (2*N),
49+
stop+ (stop-start)/ (2*N)]*2,
50+
)
51+
ax.set_title(name)
52+
fig.colorbar(im,ax=ax,aspect=7)
53+
ifname=="color zoom in":
54+
im.set_clim(-0.1,0.1)
55+
elifname=="color zoom out":
56+
im.set_clim(-10,10)
3857
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)
86-
87-
###############################################################################
88-
# Display
89-
#
90-
# The colormap will now respond to left and right clicks in the Colorbar axes
58+
im.set_clim(-1,1)
59+
60+
fornameinpan_axes:
61+
ax=ax_dict[name]
62+
ifname=="2d":
63+
# Don't make this axes twice
64+
continue
65+
im=ax.imshow(
66+
data2d,
67+
extent=[start- (stop-start)/ (2*N),
68+
stop+ (stop-start)/ (2*N)]*2,
69+
)
70+
ax.set_title(name)
71+
fig.colorbar(im,ax=ax,aspect=7)
72+
ifname=="color pan up":
73+
im.set_clim(0,2)
74+
elifname=="color pan down":
75+
im.set_clim(-2,0)
76+
else:
77+
im.set_clim(-1,1)
9178

9279
plt.show()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp