|
80 | 80 | # The following images are down-sampled from 450 data pixels to approximately
|
81 | 81 | # 125 pixels or 250 pixels (depending on your display).
|
82 | 82 | # The underlying image has alternating +1, -1 stripes on the left side, and
|
83 |
| -# a varying wavenumber (`chirp <https://en.wikipedia.org/wiki/Chirp>`_) pattern |
| 83 | +# a varying wavelength (`chirp <https://en.wikipedia.org/wiki/Chirp>`_) pattern |
84 | 84 | # in the rest of the image. If we zoom, we can see this detail without any
|
85 | 85 | # down-sampling:
|
86 | 86 |
|
|
146 | 146 |
|
147 | 147 | fig, ax = plt.subplots(figsize=(6.8, 6.8))
|
148 | 148 | ax.imshow(alarge, interpolation='nearest', cmap='grey')
|
149 |
| -ax.set_title("upsampled by factor a 1.17, interpolation='nearest'") |
| 149 | +ax.set_title("up-sampled by factor a 1.17, interpolation='nearest'") |
150 | 150 |
|
151 | 151 | # %%
|
152 |
| -# Better antialiasing algorithms can reduce this effect: |
| 152 | +# Better anti-aliasing algorithms can reduce this effect: |
153 | 153 | fig, ax = plt.subplots(figsize=(6.8, 6.8))
|
154 | 154 | ax.imshow(alarge, interpolation='antialiased', cmap='grey')
|
155 |
| -ax.set_title("upsampled by factor a 1.17, interpolation='antialiased'") |
| 155 | +ax.set_title("up-sampled by factor a 1.17, interpolation='antialiased'") |
156 | 156 |
|
157 | 157 | # %%
|
158 |
| -# Apart from the default 'hanning' antialiasing, `~.Axes.imshow` supports a |
| 158 | +# Apart from the default 'hanning' anti-aliasing, `~.Axes.imshow` supports a |
159 | 159 | # number of different interpolation algorithms, which may work better or
|
160 | 160 | # worse depending on the underlying data.
|
161 | 161 | fig, axs = plt.subplots(1, 2, figsize=(7, 4), layout='constrained')
|
|
164 | 164 | ax.set_title(f"interpolation='{interp}'")
|
165 | 165 |
|
166 | 166 | # %%
|
167 |
| -# A final example shows the desirability of performing the anti-aliasing at |
168 |
| -# the RGBA stage when using non-trivial interpolation kernels. In the following, |
169 |
| -# the data in the upper 100 rows is exactly |
170 |
| -# 0.0, and data in the inner circle is exactly 2.0. If we perform the |
171 |
| -# *interpolation_stage* in 'data' space and use an anti-aliasing filter (first |
172 |
| -# panel), then floating point imprecision makes some of the data values just a |
173 |
| -# bit less than zero or a bit more than 2.0, and they get assigned the under- |
174 |
| -# or over- colors. This can be avoided if you do not use an anti-aliasing filter |
175 |
| -# (*interpolation* set set to 'nearest'), however, that makes the part of the |
176 |
| -# data susceptible to Moiré patterns much worse (second panel). Therefore, we |
177 |
| -# recommend the default *interpolation* of 'hanning'/'antialiased', and |
178 |
| -# *interpolation_stage* of 'rgba'/'antialiased' for most down-sampling |
179 |
| -# situations (last panel). |
| 167 | +# A final example shows the desirability of performing the anti-aliasing at the |
| 168 | +# RGBA stage when using non-trivial interpolation kernels. In the following, |
| 169 | +# the data in the upper 100 rows is exactly 0.0, and data in the inner circle |
| 170 | +# is exactly 2.0. If we perform the *interpolation_stage* in 'data' space and |
| 171 | +# use an anti-aliasing filter (first panel), then floating point imprecision |
| 172 | +# makes some of the data values just a bit less than zero or a bit more than |
| 173 | +# 2.0, and they get assigned the under- or over- colors. This can be avoided if |
| 174 | +# you do not use an anti-aliasing filter (*interpolation* set set to |
| 175 | +# 'nearest'), however, that makes the part of the data susceptible to Moiré |
| 176 | +# patterns much worse (second panel). Therefore, we recommend the default |
| 177 | +# *interpolation* of 'hanning'/'antialiased', and *interpolation_stage* of |
| 178 | +# 'rgba'/'antialiased' for most down-sampling situations (last panel). |
180 | 179 |
|
181 | 180 | a = alarge + 1
|
182 | 181 | cmap = plt.get_cmap('RdBu_r')
|
|
199 | 198 | # Up-sampling
|
200 | 199 | # ===========
|
201 | 200 | #
|
202 |
| -# If we upsample, then we can represent a data pixel by many image or screen pixels. |
| 201 | +# If we up-sample, then we can represent a data pixel by many image or screen pixels. |
203 | 202 | # In the following example, we greatly over-sample the small data matrix.
|
204 | 203 |
|
205 | 204 | np.random.seed(19680801+9)
|
|
239 | 238 | # aware that some vector image viewers may smooth image pixels.
|
240 | 239 | #
|
241 | 240 | # The second method is to exactly match the size of your axes to the size of
|
242 |
| -# your data. in the following, the figure is exactly 2 inches by 2 inches, and |
243 |
| -# the dpi is 200, so the 400x400 data is not resampled at all. If you download |
| 241 | +# your data. In the following, the figure is exactly 2 inches by 2 inches, and |
| 242 | +# the dpi is 200, then the 400x400 data is not resampled at all. If you download |
244 | 243 | # this image and zoom in an image viewer you should see the individual stripes
|
245 | 244 | # on the left hand side.
|
246 | 245 |
|
247 |
| -fig = plt.figure(figsize=(2, 2)) |
| 246 | +fig = plt.figure(figsize=(2, 2), dpi=200) |
248 | 247 | ax = fig.add_axes([0, 0, 1, 1])
|
249 | 248 | ax.imshow(aa[:400, :400], cmap='RdBu_r', interpolation='nearest')
|
250 | 249 | plt.show()
|
|
0 commit comments