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

Commit5065dc3

Browse files
authored
Merge pull request#21236 from greglucas/cmap-interactive-example
DOC: Update interactive colormap example
2 parents18bfee3 +cb7e3f7 commit5065dc3

File tree

1 file changed

+18
-77
lines changed

1 file changed

+18
-77
lines changed

‎examples/images_contours_and_fields/colormap_interactive_adjustment.py

Lines changed: 18 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,90 +3,31 @@
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 bounding box of the zoom region defines the new vmin and
12+
vmax of 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 on an axis. When panning, the vmin and vmax of the norm are
15+
both shifted according to the direction of movement. The
16+
Home/Back/Forward buttons can also be used to get back to a previous state.
1217
1318
.. redirect-from:: /gallery/userdemo/colormap_interactive_adjustment
1419
"""
15-
16-
importnumpyasnp
1720
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-
21+
importnumpyasnp
4422

45-
###############################################################################
46-
# Generate figure with Axesimage and Colorbar
23+
t=np.linspace(0,2*np.pi,1024)
24+
data2d=np.sin(t)[:,np.newaxis]*np.cos(t)[np.newaxis, :]
4725

4826
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)
27+
im=ax.imshow(data2d)
28+
ax.set_title('Pan on the colorbar to shift the color mapping\n'
29+
'Zoom on the colorbar to scale the color mapping')
8630

87-
###############################################################################
88-
# Display
89-
#
90-
# The colormap will now respond to left and right clicks in the Colorbar axes
31+
fig.colorbar(im,ax=ax,label='Interactive colorbar')
9132

9233
plt.show()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp