Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0c855aa

Browse files
committed
DOC: change documentation on anti-aliasing to use the new kwarg
1 parent865f48c commit0c855aa

File tree

3 files changed

+61
-26
lines changed

3 files changed

+61
-26
lines changed

‎examples/images_contours_and_fields/image_antialiasing.py

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,21 @@
55
66
Images are represented by discrete pixels, either on the screen or in an
77
image file. When data that makes up the image has a different resolution
8-
than its representation on the screen we will see aliasing effects.
8+
than its representation on the screen we will see aliasing effects. How
9+
noticeable these are depends on how much down-sampling takes place in
10+
the change of resolution (if any)
911
10-
The default image interpolation in Matplotlib is 'antialiased'. This uses a
11-
hanning interpolation for reduced aliasing in most situations. Only when there
12-
is upsampling by a factor of 1, 2 or >=3 is 'nearest' neighbor interpolation
13-
used.
12+
When subsampling data, aliasing is reduced by smoothing first and then
13+
subsampling the smoothed data. In Matplotlib, we can do that
14+
smoothing before mapping the data to colors, or we can do the smoothing
15+
on the RGB(A) data in the final image. The difference between these is
16+
shown below, and controlled with the *interp_space* keyword argument.
17+
18+
The default image interpolation in Matplotlib is 'antialiased', and
19+
it is applied to the data. This uses a
20+
hanning interpolation on the data provided by the user for reduced aliasing
21+
in most situations. Only when there is upsampling by a factor of 1, 2 or
22+
>=3 is 'nearest' neighbor interpolation used.
1423
1524
Other anti-aliasing filters can be specified in `.Axes.imshow` using the
1625
*interpolation* keyword argument.
@@ -21,25 +30,51 @@
2130

2231
###############################################################################
2332
# First we generate a 500x500 px image with varying frequency content:
24-
x=np.arange(500)/500-0.5
25-
y=np.arange(500)/500-0.5
33+
N=450
34+
x=np.arange(N)/N-0.5
35+
y=np.arange(N)/N-0.5
36+
aa=np.ones((N,N))
37+
aa[::2, :]=-1
2638

2739
X,Y=np.meshgrid(x,y)
2840
R=np.sqrt(X**2+Y**2)
29-
f0=10
30-
k=250
41+
f0=5
42+
k=100
3143
a=np.sin(np.pi*2* (f0*R+k*R**2/2))
32-
33-
44+
# make the left hand side of this
45+
a[:int(N/2), :][R[:int(N/2), :]<0.4]=-1
46+
a[:int(N/2), :][R[:int(N/2), :]<0.3]=1
47+
aa[:,int(N/3):]=a[:,int(N/3):]
48+
a=aa
3449
###############################################################################
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
50+
# The following images are subsampled from 450 data pixels to either
51+
# 125 pixels or 250 pixels (depending on your display).
52+
# The Moire patterns in the 'nearest' interpolation are caused by the
53+
# high-frequency data being subsampled. The 'antialiased' imaged
3854
# still has some Moire patterns as well, but they are greatly reduced.
39-
fig,axs=plt.subplots(1,2,figsize=(7,4),constrained_layout=True)
40-
forax,interpinzip(axs, ['nearest','antialiased']):
41-
ax.imshow(a,interpolation=interp,cmap='gray')
42-
ax.set_title(f"interpolation='{interp}'")
55+
#
56+
# There are substantial differences between the 'data' interpolation and
57+
# the 'rgba' interpolation. The alternating bands of red and blue on the
58+
# left third of the image are subsampled. By interpolating in 'data' space
59+
# (the default) he antialiasing filter makes the stripes close to white,
60+
# because the average of -1 and +1 is zero, and zero is white in this
61+
# colormap.
62+
#
63+
# Conversely, when the anti-aliasing occurs in 'rgba' space, the red and
64+
# blue are combined visually to make purple. This behaviour is more like a
65+
# typical image processing package.
66+
67+
fig,axs=plt.subplots(2,2,figsize=(5,6),constrained_layout=True)
68+
axs[0,0].imshow(a,interpolation='nearest',cmap='RdBu_r')
69+
axs[0,0].set_xlim(100,200)
70+
axs[0,0].set_ylim(275,175)
71+
axs[0,0].set_title('Zoom')
72+
73+
forax,interp,spaceinzip(axs.flat[1:],
74+
['nearest','antialiased','antialiased'],
75+
['data','data','rgba']):
76+
ax.imshow(a,interpolation=interp,interp_space=space,cmap='RdBu_r')
77+
ax.set_title(f"interpolation='{interp}'\nspace='{space}'")
4378
plt.show()
4479

4580
###############################################################################
@@ -63,7 +98,7 @@
6398
plt.show()
6499

65100
###############################################################################
66-
# Apart from the default 'hanning' antialiasing `~.Axes.imshow` supports a
101+
# Apart from the default 'hanning' antialiasing, `~.Axes.imshow` supports a
67102
# number of different interpolation algorithms, which may work better or
68103
# worse depending on the pattern.
69104
fig,axs=plt.subplots(1,2,figsize=(7,4),constrained_layout=True)

‎lib/matplotlib/axes/_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5285,7 +5285,7 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
52855285
replace_names=["y","x1","x2","where"])
52865286

52875287
#### plotting z(x, y): imshow, pcolor and relatives, contour
5288-
@_api.make_keyword_only("3.5","aspect")
5288+
#@_api.make_keyword_only("3.5", "aspect")
52895289
@_preprocess_data()
52905290
defimshow(self,X,cmap=None,norm=None,aspect=None,
52915291
interpolation=None,alpha=None,

‎lib/matplotlib/pyplot.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,15 +2874,15 @@ def hlines(
28742874
@_copy_docstring_and_deprecators(Axes.imshow)
28752875
defimshow(
28762876
X,cmap=None,norm=None,aspect=None,interpolation=None,
2877-
interp_space=None,alpha=None,vmin=None,vmax=None,
2878-
origin=None,extent=None,*,filternorm=True,filterrad=4.0,
2877+
alpha=None,vmin=None,vmax=None,origin=None,extent=None,*,
2878+
interp_space=None,filternorm=True,filterrad=4.0,
28792879
resample=None,url=None,data=None,**kwargs):
28802880
__ret=gca().imshow(
28812881
X,cmap=cmap,norm=norm,aspect=aspect,
2882-
interpolation=interpolation,interp_space=interp_space,
2883-
alpha=alpha,vmin=vmin,vmax=vmax,origin=origin,
2884-
extent=extent,filternorm=filternorm,filterrad=filterrad,
2885-
resample=resample,url=url,
2882+
interpolation=interpolation,alpha=alpha,vmin=vmin,
2883+
vmax=vmax,origin=origin,extent=extent,
2884+
interp_space=interp_space,filternorm=filternorm,
2885+
filterrad=filterrad,resample=resample,url=url,
28862886
**({"data":data}ifdataisnotNoneelse {}),**kwargs)
28872887
sci(__ret)
28882888
return__ret

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp