|
4 | 4 | ===========
|
5 | 5 |
|
6 | 6 | Make a set of images with a single colormap, norm, and colorbar.
|
7 |
| -
|
8 |
| -It also illustrates colorbar tick labelling with a multiplier. |
9 | 7 | """
|
10 | 8 |
|
11 |
| -from matplotlib.pyplot import figure, show, sca, sci |
12 |
| -from matplotlib import cm, colors |
13 |
| -from matplotlib.font_manager import FontProperties |
| 9 | +from matplotlib import colors, pyplot as plt |
14 | 10 | import numpy as np
|
15 | 11 |
|
| 12 | +np.random.seed(19680801) |
16 | 13 | Nr = 3
|
17 | 14 | Nc = 2
|
| 15 | +cmap = "cool" |
18 | 16 |
|
19 |
| -fig = figure() |
20 |
| -cmap = cm.cool |
21 |
| - |
22 |
| -figtitle = 'Multiple images' |
23 |
| -t = fig.text(0.5, 0.95, figtitle, |
24 |
| - horizontalalignment='center', |
25 |
| - fontproperties=FontProperties(size=16)) |
26 |
| - |
27 |
| -cax = fig.add_axes([0.2, 0.08, 0.6, 0.04]) |
| 17 | +fig, axs = plt.subplots(Nr, Nc) |
| 18 | +fig.suptitle('Multiple images') |
28 | 19 |
|
29 |
| -w = 0.4 |
30 |
| -h = 0.22 |
31 |
| -ax = [] |
32 | 20 | images = []
|
33 |
| -vmin = 1e40 |
34 |
| -vmax = -1e40 |
35 | 21 | for i in range(Nr):
|
36 | 22 | for j in range(Nc):
|
37 |
| - pos = [0.075 + j * 1.1 * w, 0.18 + i * 1.2 * h, w, h] |
38 |
| - a = fig.add_axes(pos) |
39 |
| - if i > 0: |
40 |
| - a.set_xticklabels([]) |
41 |
| - # Make some fake data with a range that varies |
42 |
| - # somewhat from one plot to the next. |
| 23 | + # Generate data with a range that varies from one plot to the next. |
43 | 24 | data = ((1 + i + j) / 10) * np.random.rand(10, 20) * 1e-6
|
44 |
| - dd = data.ravel() |
45 |
| - # Manually find the min and max of all colors for |
46 |
| - # use in setting the color scale. |
47 |
| - vmin = min(vmin, np.min(dd)) |
48 |
| - vmax = max(vmax, np.max(dd)) |
49 |
| - images.append(a.imshow(data, cmap=cmap)) |
50 |
| - |
51 |
| - ax.append(a) |
52 |
| - |
53 |
| -# Set the first image as the master, with all the others |
54 |
| -# observing it for changes in cmap or norm. |
55 |
| - |
56 |
| - |
57 |
| -class ImageFollower(object): |
58 |
| - 'update image in response to changes in clim or cmap on another image' |
59 |
| - |
60 |
| - def __init__(self, follower): |
61 |
| - self.follower = follower |
62 |
| - |
63 |
| - def __call__(self, leader): |
64 |
| - self.follower.set_cmap(leader.get_cmap()) |
65 |
| - self.follower.set_clim(leader.get_clim()) |
66 |
| - |
| 25 | + images.append(axs[i, j].imshow(data, cmap=cmap)) |
| 26 | + axs[i, j].label_outer() |
67 | 27 |
|
| 28 | +# Find the min and max of all colors for use in setting the color scale. |
| 29 | +vmin = min(image.get_array().min() for image in images) |
| 30 | +vmax = max(image.get_array().max() for image in images) |
68 | 31 | norm = colors.Normalize(vmin=vmin, vmax=vmax)
|
69 |
| -for i, im in enumerate(images): |
| 32 | +for im in images: |
70 | 33 | im.set_norm(norm)
|
71 |
| - if i > 0: |
72 |
| - images[0].callbacksSM.connect('changed', ImageFollower(im)) |
73 | 34 |
|
74 |
| -# The colorbar is also based on this master image. |
75 |
| -fig.colorbar(images[0], cax, orientation='horizontal') |
| 35 | +fig.colorbar(images[0], ax=axs, orientation='horizontal', fraction=.1) |
| 36 | + |
| 37 | + |
| 38 | +# Make images respond to changes in the norm of other images (e.g. via the |
| 39 | +# "edit axis, curves and images parameters" GUI on Qt), but be careful not to |
| 40 | +# recurse infinitely! |
| 41 | +def update(changed_image): |
| 42 | + for im in images: |
| 43 | + if (changed_image.get_cmap() != im.get_cmap() |
| 44 | + or changed_image.get_clim() != im.get_clim()): |
| 45 | + im.set_cmap(changed_image.get_cmap()) |
| 46 | + im.set_clim(changed_image.get_clim()) |
76 | 47 |
|
77 |
| -# We need the following only if we want to run this interactively and |
78 |
| -# modify the colormap: |
79 | 48 |
|
80 |
| -sca(ax[0]) # Return the current axes to the first one, |
81 |
| -sci(images[0]) # because the current image must be in current axes. |
| 49 | +for im in images: |
| 50 | + im.callbacksSM.connect('changed', update) |
82 | 51 |
|
83 |
| -show() |
| 52 | +plt.show() |
0 commit comments