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

Commit63b48cc

Browse files
committed
DOC: Clarify/simplify example of multiple images with one colorbar
1 parent199c31f commit63b48cc

File tree

1 file changed

+48
-37
lines changed

1 file changed

+48
-37
lines changed
Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
"""
2-
===============
3-
Multiple images
4-
===============
2+
=================================
3+
Multiple images with one colorbar
4+
=================================
55
6-
Make a set of images with a single colormap, norm, and colorbar.
6+
Use a single colorbar for multiple images.
7+
8+
Currently, a colorbar can only be associated with one image. To ensure that
9+
the colorbar is representative for multiple images, we have to ensure that
10+
all images use the same color mapping. The most important aspect is the
11+
data normalization. By explicitly creating a norm and using that for all
12+
images, we ensure that all images are scaled consistently.
713
"""
814

915
importmatplotlib.pyplotasplt
@@ -12,47 +18,54 @@
1218
frommatplotlibimportcolors
1319

1420
np.random.seed(19680801)
15-
Nr=3
16-
Nc=2
1721

18-
fig,axs=plt.subplots(Nr,Nc)
22+
datasets= [
23+
(i+1)/10*np.random.rand(10,20)
24+
foriinrange(4)
25+
]
26+
27+
fig,axs=plt.subplots(2,2)
1928
fig.suptitle('Multiple images')
2029

21-
images= []
22-
foriinrange(Nr):
23-
forjinrange(Nc):
24-
# Generate data with a range that varies from one plot to the next.
25-
data= ((1+i+j)/10)*np.random.rand(10,20)
26-
images.append(axs[i,j].imshow(data))
27-
axs[i,j].label_outer()
30+
# create a single norm to be shared across all images
31+
norm=colors.Normalize(vmin=np.min(datasets),vmax=np.max(datasets))
2832

29-
# Find the min and max of all colors for use in setting the color scale.
30-
vmin=min(image.get_array().min()forimageinimages)
31-
vmax=max(image.get_array().max()forimageinimages)
32-
norm=colors.Normalize(vmin=vmin,vmax=vmax)
33-
foriminimages:
34-
im.set_norm(norm)
33+
images= []
34+
forax,datainzip(axs.flat,datasets):
35+
images.append(ax.imshow(data,norm=norm))
3536

3637
fig.colorbar(images[0],ax=axs,orientation='horizontal',fraction=.1)
3738

38-
39-
# Make images respond to changes in the norm of other images (e.g. via the
40-
# "edit axis, curves and images parameters" GUI on Qt), but be careful not to
41-
# recurse infinitely!
42-
defupdate(changed_image):
43-
foriminimages:
44-
if (changed_image.get_cmap()!=im.get_cmap()
45-
orchanged_image.get_clim()!=im.get_clim()):
46-
im.set_cmap(changed_image.get_cmap())
47-
im.set_clim(changed_image.get_clim())
48-
49-
50-
foriminimages:
51-
im.callbacks.connect('changed',update)
52-
5339
plt.show()
5440

5541
# %%
42+
# The colors are now kept consistent across all images when changing the
43+
# scaling, e.g. through zooming in the colorbar or via the "edit axis,
44+
# curves and images parameters" GUI of the Qt backend. This is sufficient
45+
# for most practical use cases.
46+
#
47+
# Advanced: Additionally sync the colormap
48+
# ----------------------------------------
49+
#
50+
# While the norm is shared, the colormaps are not. This is often ok
51+
# because colormaps are usually not changed dynamically. However, a user
52+
# could change the colormap of an individual image through the
53+
# "edit axis, curves and images parameters" GUI of the Qt backend.
54+
# Unlike with a norm, it does not help to share colormaps between images.
55+
# Changes to the norm limits modify the norm object in place and thus
56+
# propagate to all images. But changing a colormap sets a new colormap
57+
# object to the images and thus does not propagate to the other images.
58+
# To make all other images follow, you could additionally sync the
59+
# colormaps using the following code::
60+
#
61+
# def sync_cmaps(changed_image):
62+
# for im in images:
63+
# if changed_image.get_cmap() != im.get_cmap():
64+
# im.set_cmap(changed_image.get_cmap())
65+
#
66+
# for im in images:
67+
# im.callbacks.connect('changed', sync_cmaps)
68+
#
5669
#
5770
# .. admonition:: References
5871
#
@@ -63,6 +76,4 @@ def update(changed_image):
6376
# - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar`
6477
# - `matplotlib.colors.Normalize`
6578
# - `matplotlib.cm.ScalarMappable.set_cmap`
66-
# - `matplotlib.cm.ScalarMappable.set_norm`
67-
# - `matplotlib.cm.ScalarMappable.set_clim`
6879
# - `matplotlib.cbook.CallbackRegistry.connect`

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp