|
1 | 1 | """
|
2 |
| -=================== |
3 |
| -Displaying an image |
4 |
| -=================== |
| 2 | +========== |
| 3 | +Image Demo |
| 4 | +========== |
| 5 | +
|
| 6 | +Many ways to plot images in Matplotlib. |
| 7 | +
|
| 8 | +The most common way to plot images in Matplotlib is with |
| 9 | +imshow. The following examples demonstrate much of the |
| 10 | +functionality of imshow and the many images you can create. |
5 | 11 |
|
6 |
| -Simple demo of the imshow function. |
7 | 12 | """
|
| 13 | +from __future__ import print_function |
| 14 | + |
| 15 | +import numpy as np |
| 16 | +import matplotlib.cm as cm |
| 17 | +import matplotlib.mlab as mlab |
8 | 18 | import matplotlib.pyplot as plt
|
9 | 19 | import matplotlib.cbook as cbook
|
| 20 | +from matplotlib.path import Path |
| 21 | +from matplotlib.patches import PathPatch |
| 22 | + |
| 23 | +############################################################################### |
| 24 | +# First we'll generate a simple bivariate normal distribution. |
| 25 | + |
| 26 | +delta = 0.025 |
| 27 | +x = y = np.arange(-3.0, 3.0, delta) |
| 28 | +X, Y = np.meshgrid(x, y) |
| 29 | +Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) |
| 30 | +Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) |
| 31 | +Z = Z2 - Z1 # difference of Gaussians |
| 32 | + |
| 33 | +im = plt.imshow(Z, interpolation='bilinear', cmap=cm.RdYlGn, |
| 34 | + origin='lower', extent=[-3, 3, -3, 3], |
| 35 | + vmax=abs(Z).max(), vmin=-abs(Z).max()) |
| 36 | + |
| 37 | +plt.show() |
| 38 | + |
| 39 | + |
| 40 | +############################################################################### |
| 41 | +# It is also possible to show images of pictures. |
10 | 42 |
|
| 43 | +# A sample image |
11 | 44 | image_file = cbook.get_sample_data('ada.png')
|
12 | 45 | image = plt.imread(image_file)
|
13 | 46 |
|
14 | 47 | fig, ax = plt.subplots()
|
15 | 48 | ax.imshow(image)
|
16 | 49 | ax.axis('off') # clear x- and y-axes
|
| 50 | + |
| 51 | + |
| 52 | +# And another image |
| 53 | + |
| 54 | +w, h = 512, 512 |
| 55 | + |
| 56 | +datafile = cbook.get_sample_data('ct.raw.gz', asfileobj=True) |
| 57 | +s = datafile.read() |
| 58 | +A = np.fromstring(s, np.uint16).astype(float).reshape((w, h)) |
| 59 | +A /= A.max() |
| 60 | + |
| 61 | +extent = (0, 25, 0, 25) |
| 62 | +im = plt.imshow(A, cmap=plt.cm.hot, origin='upper', extent=extent) |
| 63 | + |
| 64 | +markers = [(15.9, 14.5), (16.8, 15)] |
| 65 | +x, y = zip(*markers) |
| 66 | +plt.plot(x, y, 'o') |
| 67 | + |
| 68 | +plt.title('CT density') |
| 69 | + |
| 70 | +plt.show() |
| 71 | + |
| 72 | + |
| 73 | +############################################################################### |
| 74 | +# Interpolating images |
| 75 | +# -------------------- |
| 76 | +# |
| 77 | +# It is also possible to interpolate images before displaying them. Be careful, |
| 78 | +# as this may manipulate the way your data looks, but it can be helpful for |
| 79 | +# achieving the look you want. Below we'll display the same (small) array, |
| 80 | +# interpolated with three different interpolation methods. |
| 81 | +# |
| 82 | +# The center of the pixel at A[i,j] is plotted at i+0.5, i+0.5. If you |
| 83 | +# are using interpolation='nearest', the region bounded by (i,j) and |
| 84 | +# (i+1,j+1) will have the same color. If you are using interpolation, |
| 85 | +# the pixel center will have the same color as it does with nearest, but |
| 86 | +# other pixels will be interpolated between the neighboring pixels. |
| 87 | +# |
| 88 | +# Earlier versions of matplotlib (<0.63) tried to hide the edge effects |
| 89 | +# from you by setting the view limits so that they would not be visible. |
| 90 | +# A recent bugfix in antigrain, and a new implementation in the |
| 91 | +# matplotlib._image module which takes advantage of this fix, no longer |
| 92 | +# makes this necessary. To prevent edge effects, when doing |
| 93 | +# interpolation, the matplotlib._image module now pads the input array |
| 94 | +# with identical pixels around the edge. e.g., if you have a 5x5 array |
| 95 | +# with colors a-y as below:: |
| 96 | +# |
| 97 | +# a b c d e |
| 98 | +# f g h i j |
| 99 | +# k l m n o |
| 100 | +# p q r s t |
| 101 | +# u v w x y |
| 102 | +# |
| 103 | +# the _image module creates the padded array,:: |
| 104 | +# |
| 105 | +# a a b c d e e |
| 106 | +# a a b c d e e |
| 107 | +# f f g h i j j |
| 108 | +# k k l m n o o |
| 109 | +# p p q r s t t |
| 110 | +# o u v w x y y |
| 111 | +# o u v w x y y |
| 112 | +# |
| 113 | +# does the interpolation/resizing, and then extracts the central region. |
| 114 | +# This allows you to plot the full range of your array w/o edge effects, |
| 115 | +# and for example to layer multiple images of different sizes over one |
| 116 | +# another with different interpolation methods - see |
| 117 | +# examples/layer_images.py. It also implies a performance hit, as this |
| 118 | +# new temporary, padded array must be created. Sophisticated |
| 119 | +# interpolation also implies a performance hit, so if you need maximal |
| 120 | +# performance or have very large images, interpolation='nearest' is |
| 121 | +# suggested. |
| 122 | + |
| 123 | +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 | + |
| 132 | +plt.figure(3) |
| 133 | +plt.imshow(A, interpolation='bicubic') |
| 134 | +plt.grid(True) |
| 135 | + |
| 136 | +plt.show() |
| 137 | + |
| 138 | + |
| 139 | +############################################################################### |
| 140 | +# 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 |
| 144 | + |
| 145 | +x = np.arange(120).reshape((10, 12)) |
| 146 | + |
| 147 | +interp = 'bilinear' |
| 148 | +fig, axs = plt.subplots(nrows=2, sharex=True, figsize=(3, 5)) |
| 149 | +axs[0].set_title('blue should be up') |
| 150 | +axs[0].imshow(x, origin='upper', interpolation=interp) |
| 151 | + |
| 152 | +axs[1].set_title('blue should be down') |
| 153 | +axs[1].imshow(x, origin='lower', interpolation=interp) |
| 154 | +plt.show() |
| 155 | + |
| 156 | + |
| 157 | +############################################################################### |
| 158 | +# Finally, we'll show an image using a clip path. |
| 159 | + |
| 160 | +delta = 0.025 |
| 161 | +x = y = np.arange(-3.0, 3.0, delta) |
| 162 | +X, Y = np.meshgrid(x, y) |
| 163 | +Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) |
| 164 | +Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) |
| 165 | +Z = Z2 - Z1 # difference of Gaussians |
| 166 | + |
| 167 | +path = Path([[0, 1], [1, 0], [0, -1], [-1, 0], [0, 1]]) |
| 168 | +patch = PathPatch(path, facecolor='none') |
| 169 | +plt.gca().add_patch(patch) |
| 170 | + |
| 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) |
| 174 | +im.set_clip_path(patch) |
| 175 | + |
17 | 176 | plt.show()
|
0 commit comments