|
41 | 41 | # It is also possible to show images of pictures. |
42 | 42 |
|
43 | 43 | # A sample image |
44 | | -image_file = cbook.get_sample_data('ada.png') |
45 | | -image = plt.imread(image_file) |
| 44 | +with cbook.get_sample_data('ada.png') as image_file: |
| 45 | + image = plt.imread(image_file) |
46 | 46 |
|
47 | 47 | fig, ax = plt.subplots() |
48 | 48 | ax.imshow(image) |
|
53 | 53 |
|
54 | 54 | w, h = 512, 512 |
55 | 55 |
|
56 | | -datafile = cbook.get_sample_data('ct.raw.gz', asfileobj=True) |
57 | | -s = datafile.read() |
| 56 | +with cbook.get_sample_data('ct.raw.gz', asfileobj=True) as datafile: |
| 57 | + s = datafile.read() |
58 | 58 | A = np.fromstring(s, np.uint16).astype(float).reshape((w, h)) |
59 | 59 | A /= A.max() |
60 | 60 |
|
| 61 | +fig, ax = plt.subplots() |
61 | 62 | extent = (0, 25, 0, 25) |
62 | | -im = plt.imshow(A, cmap=plt.cm.hot, origin='upper', extent=extent) |
| 63 | +im = ax.imshow(A, cmap=plt.cm.hot, origin='upper', extent=extent) |
63 | 64 |
|
64 | 65 | markers = [(15.9, 14.5), (16.8, 15)] |
65 | 66 | x, y = zip(*markers) |
66 | | -plt.plot(x, y, 'o') |
| 67 | +ax.plot(x, y, 'o') |
67 | 68 |
|
68 | | -plt.title('CT density') |
| 69 | +ax.set_title('CT density') |
69 | 70 |
|
70 | 71 | plt.show() |
71 | 72 |
|
|
121 | 122 | # suggested. |
122 | 123 |
|
123 | 124 | A = np.random.rand(5, 5) |
124 | | -plt.figure(1) |
125 | | -plt.imshow(A, interpolation='nearest') |
126 | | -plt.grid(True) |
127 | | - |
128 | | -plt.figure(2) |
129 | | -plt.imshow(A, interpolation='bilinear') |
130 | | -plt.grid(True) |
131 | 125 |
|
132 | | -plt.figure(3) |
133 | | -plt.imshow(A, interpolation='bicubic') |
134 | | -plt.grid(True) |
| 126 | +fig, axs = plt.subplots(1, 3, figsize=(10, 3)) |
| 127 | +for ax, interp in zip(axs, ['nearest', 'bilinear', 'bicubic']): |
| 128 | + ax.imshow(A, interpolation=interp) |
| 129 | + ax.set_title(interp.capitalize()) |
| 130 | + ax.grid(True) |
135 | 131 |
|
136 | 132 | plt.show() |
137 | 133 |
|
138 | 134 |
|
139 | 135 | ############################################################################### |
140 | 136 | # You can specify whether images should be plotted with the array origin |
141 | | -# x[0,0] in the upper left or upper right by using the origin parameter. |
142 | | -# You can also control the default be setting image.origin in your |
143 | | -# matplotlibrc file; see http://matplotlib.org/matplotlibrc |
| 137 | +# x[0,0] in the upper left or lower right by using the origin parameter. |
| 138 | +# You can also control the default setting image.origin in your |
| 139 | +# :ref:`matplotlibrc file <customizing-with-matplotlibrc-files>` |
144 | 140 |
|
145 | 141 | x = np.arange(120).reshape((10, 12)) |
146 | 142 |
|
|
166 | 162 |
|
167 | 163 | path = Path([[0, 1], [1, 0], [0, -1], [-1, 0], [0, 1]]) |
168 | 164 | patch = PathPatch(path, facecolor='none') |
169 | | -plt.gca().add_patch(patch) |
170 | 165 |
|
171 | | -im = plt.imshow(Z, interpolation='bilinear', cmap=cm.gray, |
172 | | - origin='lower', extent=[-3, 3, -3, 3], |
173 | | - clip_path=patch, clip_on=True) |
| 166 | +fig, ax = plt.subplots() |
| 167 | +ax.add_patch(patch) |
| 168 | + |
| 169 | +im = ax.imshow(Z, interpolation='bilinear', cmap=cm.gray, |
| 170 | + origin='lower', extent=[-3, 3, -3, 3], |
| 171 | + clip_path=patch, clip_on=True) |
174 | 172 | im.set_clip_path(patch) |
175 | 173 |
|
176 | 174 | plt.show() |
0 commit comments