import
numpy
as
np
,
=
(
1
,
2
,
figsize
=
(
4
,
2
))
=
450
=
(
)
/
-
0.5
=
(
)
/
-
0.5
=
((
,
))
[::
2
,
:]
=
-
1
,
=
(
,
)
=
(
**
2
+
**
2
)
=
5
=
100
=
(
*
2
*
(
*
+
*
**
2
/
2
))
[:
int
(
/
2
),
:][
[:
int
(
/
2
),
:]
<
0.4
]
=
-
1
[:
int
(
/
2
),
:][
[:
int
(
/
2
),
:]
<
0.3
]
=
1
[:,
int
(
/
3
):]
=
[:,
int
(
/
3
):]
=
[
0
]
.
imshow
(
,
=
'RdBu_r'
)
[
0
]
.
set_title
(
'(450, 450) Down-sampled'
,
fontsize
=
'medium'
)
(
19680801
+
9
)
=
(
4
,
4
)
[
1
]
.
imshow
(
,
=
'viridis'
)
[
1
]
.
set_title
(
'(4, 4) Up-sampled'
,
fontsize
=
'medium'
)
Matplotlib's imshow method has two keyword arguments to allow the user
to control how resampling is done. The interpolation keyword argument allows
a choice of the kernel that is used for resampling, allowing either anti-alias filtering if
down-sampling, or smoothing of pixels if up-sampling. The
interpolation_stage keyword argument, determines if this smoothing kernel is
applied to the underlying data, or if the kernel is applied to the RGBA pixels.
interpolation_stage='rgba': Data -> Normalize -> RGBA -> Interpolate/Resample
interpolation_stage='data': Data -> Interpolate/Resample -> Normalize -> RGBA
For both keyword arguments, Matplotlib has a default "auto", that is
recommended for most situations, and is described below. Note that this
default behaves differently if the image is being down- or up-sampled, as
described below.
Down-sampling and modest up-sampling
When down-sampling data, we usually want to remove aliasing by smoothing the
image first and then sub-sampling it. In Matplotlib, we can do that smoothing
before mapping the data to colors, or we can do the smoothing on the RGB(A)
image pixels. The differences between these are shown below, and controlled
with the interpolation_stage keyword argument.
The following images are down-sampled from 450 data pixels to approximately
125 pixels or 250 pixels (depending on your display).
The underlying image has alternating +1, -1 stripes on the left side, and
a varying wavelength (chirp) pattern
in the rest of the image. If we zoom, we can see this detail without any
down-sampling:

If we down-sample, the simplest algorithm is to decimate the data using
nearest-neighbor interpolation. We can
do this in either data space or RGBA space:

Nearest interpolation is identical in data and RGBA space, and both exhibit
Moiré patterns because the
high-frequency data is being down-sampled and shows up as lower frequency
patterns. We can reduce the Moiré patterns by applying an anti-aliasing filter
to the image before rendering:

The Hanning filter smooths
the underlying data so that each new pixel is a weighted average of the
original underlying pixels. This greatly reduces the Moiré patterns.
However, when the interpolation_stage is set to 'data', it also introduces
white regions to the image that are not in the original data, both in the
alternating bands on the left hand side of the image, and in the boundary
between the red and blue of the large circles in the middle of the image.
The interpolation at the 'rgba' stage has a different artifact, with the alternating
bands coming out a shade of purple; even though purple is not in the original
colormap, it is what we perceive when a blue and red stripe are close to each
other.
The default for the interpolation keyword argument is 'auto' which
will choose a Hanning filter if the image is being down-sampled or up-sampled
by less than a factor of three. The default interpolation_stage keyword
argument is also 'auto', and for images that are down-sampled or
up-sampled by less than a factor of three it defaults to 'rgba'
interpolation.
Anti-aliasing filtering is needed, even when up-sampling. The following image
up-samples 450 data pixels to 530 rendered pixels. You may note a grid of
line-like artifacts which stem from the extra pixels that had to be made up.
Since interpolation is 'nearest' they are the same as a neighboring line of
pixels and thus stretch the image locally so that it looks distorted.

Better anti-aliasing algorithms can reduce this effect:

Apart from the default 'hanning' anti-aliasing, imshow supports a
number of different interpolation algorithms, which may work better or
worse depending on the underlying data.

A final example shows the desirability of performing the anti-aliasing at the
RGBA stage when using non-trivial interpolation kernels. In the following,
the data in the outer circle is exactly 0.0, and data in the inner circle
is exactly 2.0. If we perform the interpolation_stage in 'data' space and
use an anti-aliasing filter (first panel), then floating point imprecision
makes some of the data values just a bit less than zero or a bit more than
2.0, and they get assigned the under- or over- colors. This can be avoided if
you do not use an anti-aliasing filter (interpolation set to
'nearest'), however, that makes the part of the data susceptible to Moiré
patterns much worse (second panel). Therefore, we recommend the default
interpolation of 'hanning'/'auto', and interpolation_stage of
'rgba'/'auto' for most down-sampling situations (last panel).

ax
,
,
in
zip
(
,
[
'nearest'
,
'nearest'
],
[
'data'
,
'rgba'
]):
(
,
interpolation
=
,
interpolation_stage
=
,
=
'RdBu_r'
)
(
f
"interpolation='
{
}
'
\n
stage='
{
}
'"
)
ax
,
,
in
zip
(
,
[
'hanning'
,
'hanning'
],
[
'data'
,
'rgba'
]):
(
,
interpolation
=
,
interpolation_stage
=
,
=
'RdBu_r'
)
(
f
"interpolation='
{
}
'
\n
stage='
{
}
'"
)
()
interpolation
=
'nearest'
,
=
'grey'
)
(
"up-sampled by factor a 1.17, interpolation='nearest'"
)
interpolation
=
'auto'
,
=
'grey'
)
(
"up-sampled by factor a 1.17, interpolation='auto'"
)
ax
,
in
zip
(
,
[
'hanning'
,
'lanczos'
]):
(
,
interpolation
=
,
=
'gray'
)
(
f
"interpolation='
{
}
'"
)
cmap
.
set_over
(
'limegreen'
)
,
=
(
1
,
3
,
figsize
=
(
7
,
3
),
layout
=
'constrained'
)
for
,
,
in
zip
(
,
[
'hanning'
,
'nearest'
,
'hanning'
,
],
[
'data'
,
'data'
,
'rgba'
]):
=
(
,
interpolation
=
,
interpolation_stage
=
,
=
,
vmin
=
0
,
vmax
=
2
)
=
f
"interpolation='
{
}
'
\n
stage='
{
}
'"
if
==
[
2
]:
+=
'
\n
Default'
(
,
fontsize
=
'medium'
)
(
,
=
,
extend
=
'both'
,
shrink
=
0.8
)
[
0
]
.
imshow
(
,
=
'viridis'
,
interpolation
=
'sinc'
,
interpolation_stage
=
'data'
)
[
0
]
.
set_title
(
"interpolation='sinc'
\n
stage='data'
\n
(default for upsampling)"
)
[
1
]
.
imshow
(
,
=
'viridis'
,
interpolation
=
'sinc'
,
interpolation_stage
=
'rgba'
)
[
1
]
.
set_title
(
"interpolation='sinc'
\n
stage='rgba'"
)
(
,
=
,
shrink
=
0.7
,
extend
=
'both'
)
Download zipped: image_antialiasing.zip