Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a533b7

Browse files
committed
Update multi_image example.
Make all images respond to changes in clims or cmaps of other images, rather than just following the first image. General style modernization.
1 parent af1197d commit 9a533b7

File tree

1 file changed

+27
-58
lines changed

1 file changed

+27
-58
lines changed

examples/images_contours_and_fields/multi_image.py

Lines changed: 27 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,80 +4,49 @@
44
===========
55
66
Make a set of images with a single colormap, norm, and colorbar.
7-
8-
It also illustrates colorbar tick labelling with a multiplier.
97
"""
108

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
1410
import numpy as np
1511

12+
np.random.seed(19680801)
1613
Nr = 3
1714
Nc = 2
15+
cmap = "cool"
1816

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')
2819

29-
w = 0.4
30-
h = 0.22
31-
ax = []
3220
images = []
33-
vmin = 1e40
34-
vmax = -1e40
3521
for i in range(Nr):
3622
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.
4324
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()
6727

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)
6831
norm = colors.Normalize(vmin=vmin, vmax=vmax)
69-
for i, im in enumerate(images):
32+
for im in images:
7033
im.set_norm(norm)
71-
if i > 0:
72-
images[0].callbacksSM.connect('changed', ImageFollower(im))
7334

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())
7647

77-
# We need the following only if we want to run this interactively and
78-
# modify the colormap:
7948

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)
8251

83-
show()
52+
plt.show()

0 commit comments

Comments
 (0)