Skip to content

Commit f344400

Browse files
authored
Merge pull request #15522 from meeseeksmachine/auto-backport-of-pr-15500-on-v3.2.x
Backport PR #15500 on branch v3.2.x (Improve antialiasing example)
2 parents e78a098 + 080d583 commit f344400

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

examples/images_contours_and_fields/image_antialiasing.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
==================
3-
Image Antialiasing
3+
Image antialiasing
44
==================
55
66
Images are represented by discrete pixels, either on the screen or in an
@@ -13,14 +13,14 @@
1313
used.
1414
1515
Other anti-aliasing filters can be specified in `.Axes.imshow` using the
16-
*interpolation* kwarg.
16+
*interpolation* keyword argument.
1717
"""
1818

1919
import numpy as np
2020
import matplotlib.pyplot as plt
2121

2222
###############################################################################
23-
# First we generate an image with varying frequency content:
23+
# First we generate a 500x500 px image with varying frequency content:
2424
x = np.arange(500) / 500 - 0.5
2525
y = np.arange(500) / 500 - 0.5
2626

@@ -32,38 +32,44 @@
3232

3333

3434
###############################################################################
35-
# The following images are subsampled from 1000 data pixels to 604 rendered
36-
# pixels. The Moire patterns in the "nearest" interpolation are caused by the
37-
# high-frequency data being subsampled. The "antialiased" image
35+
# The following images are subsampled from 500 data pixels to 303 rendered
36+
# pixels. The Moire patterns in the 'nearest' interpolation are caused by the
37+
# high-frequency data being subsampled. The 'antialiased' image
3838
# still has some Moire patterns as well, but they are greatly reduced.
3939
fig, axs = plt.subplots(1, 2, figsize=(7, 4), constrained_layout=True)
40-
for n, interp in enumerate(['nearest', 'antialiased']):
41-
im = axs[n].imshow(a, interpolation=interp, cmap='gray')
42-
axs[n].set_title(interp)
40+
for ax, interp in zip(axs, ['nearest', 'antialiased']):
41+
ax.imshow(a, interpolation=interp, cmap='gray')
42+
ax.set_title(f"interpolation='{interp}'")
4343
plt.show()
4444

4545
###############################################################################
46-
# Even up-sampling an image will lead to Moire patterns unless the upsample
47-
# is an integer number of pixels.
48-
fig, ax = plt.subplots(1, 1, figsize=(5.3, 5.3))
49-
ax.set_position([0, 0, 1, 1])
50-
im = ax.imshow(a, interpolation='nearest', cmap='gray')
46+
# Even up-sampling an image with 'nearest' interpolation will lead to Moire
47+
# patterns when the upsampling factor is not integer. The following image
48+
# upsamples 500 data pixels to 530 rendered pixels. You may note a grid of
49+
# 30 line-like artifacts which stem from the 524 - 500 = 24 extra pixels that
50+
# had to be made up. Since interpolation is 'nearest' they are the same as a
51+
# neighboring line of pixels and thus stretch the image locally so that it
52+
# looks distorted.
53+
fig, ax = plt.subplots(figsize=(6.8, 6.8))
54+
ax.imshow(a, interpolation='nearest', cmap='gray')
55+
ax.set_title("upsampled by factor a 1.048, interpolation='nearest'")
5156
plt.show()
5257

5358
###############################################################################
54-
# The patterns aren't as bad, but still benefit from anti-aliasing
55-
fig, ax = plt.subplots(1, 1, figsize=(5.3, 5.3))
56-
ax.set_position([0, 0, 1, 1])
57-
im = ax.imshow(a, interpolation='antialiased', cmap='gray')
59+
# Better antialiasing algorithms can reduce this effect:
60+
fig, ax = plt.subplots(figsize=(6.8, 6.8))
61+
ax.imshow(a, interpolation='antialiased', cmap='gray')
62+
ax.set_title("upsampled by factor a 1.048, interpolation='antialiased'")
5863
plt.show()
5964

6065
###############################################################################
61-
# If the small Moire patterns in the default "hanning" antialiasing are
62-
# still undesireable, then we can use other filters.
66+
# Apart from the default 'hanning' antialiasing `~.Axes.imshow` supports a
67+
# number of different interpolation algorithms, which may work better or
68+
# worse depending on the pattern.
6369
fig, axs = plt.subplots(1, 2, figsize=(7, 4), constrained_layout=True)
64-
for n, interp in enumerate(['hanning', 'lanczos']):
65-
im = axs[n].imshow(a, interpolation=interp, cmap='gray')
66-
axs[n].set_title(interp)
70+
for ax, interp in zip(axs, ['hanning', 'lanczos']):
71+
ax.imshow(a, interpolation=interp, cmap='gray')
72+
ax.set_title(f"interpolation='{interp}'")
6773
plt.show()
6874

6975

0 commit comments

Comments
 (0)