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
915import matplotlib .pyplot as plt
1218from matplotlib import colors
1319
1420np .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+ for i in range (4 )
25+ ]
26+
27+ fig , axs = plt .subplots (2 , 2 )
1928fig .suptitle ('Multiple images' )
2029
21- images = []
22- for i in range (Nr ):
23- for j in range (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 () for image in images )
31- vmax = max (image .get_array ().max () for image in images )
32- norm = colors .Normalize (vmin = vmin , vmax = vmax )
33- for im in images :
34- im .set_norm (norm )
33+ images = []
34+ for ax , data in zip (axs .flat , datasets ):
35+ images .append (ax .imshow (data , norm = norm ))
3536
3637fig .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- def update (changed_image ):
43- for im in images :
44- if (changed_image .get_cmap () != im .get_cmap ()
45- or changed_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- for im in images :
51- im .callbacks .connect ('changed' , update )
52-
5339plt .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