注:我在写这个问题时找到了解决方案。我的回答如下。
有没有一种简单的方法可以使用 matplotlib 获得圆的亚像素抗锯齿放置?我能够创建以下 .gif,但圆圈的运动以整数像素为单位这一事实确实困扰着我。
我当然可以渲染一个大图像(plt.savefig("image.png",dpi=1000)
)并缩小规模,但增加的复杂性是一种痛苦(这是给学生的例子,所以导入额外的工具会惹恼学生,安装额外的工具会惹恼校园IT!)
import matplotlib.pyplot as plt
from math import sin,cos
x=[]
y=[]
#Create 41**2 particles in the unit square
for i in range(41):
for j in range(41):
x.append(i*0.05-1.0)
y.append(j*0.05-1.0)
#5 second video at 30 fps = 150 frames
for n in range(150):
plt.close('all')
fig, axes = plt.subplots(figsize=(5,5))
#some cool motion
for i in range(len(x)):
x[i]+=0.001*cos(x[i]+3*y[i])
y[i]+=0.001*sin(6*x[i]-4*y[i])
#create the scatter plot
axes.scatter(x,y,s=3,antialiased=True)
axes.set_xlim(-1.4,1.4)
axes.set_ylim(-1.4,1.4)
axes.set_aspect('equal')
plt.savefig("out/fig%03d.png"%n,dpi=80)
必须优化分散函数才能快速进行整数放置。如果您手动添加 Circle 对象,则会为您处理子像素抗锯齿。速度慢了很多。例子:
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from math import sin,cos
x=[]
y=[]
#Create 41**2 particles in the unit square
for i in range(41):
for j in range(41):
x.append(i*0.05-1.0)
y.append(j*0.05-1.0)
#5 second video at 30 fps = 150 frames
for n in range(150):
plt.close('all')
fig, axes = plt.subplots(figsize=(5,5))
#adding the Circles to the plot using a PatchCollection is much faster.
patches=[]
#some cool motion
for i in range(len(x)):
x[i]+=0.001*cos(x[i]+3*y[i])
y[i]+=0.001*sin(6*x[i]-4*y[i])
patches.append(plt.Circle((x[i],y[i]),0.015))
axes.add_collection(PatchCollection(patches, alpha=0.95))
axes.set_xlim(-1.4,1.4)
axes.set_ylim(-1.4,1.4)
axes.set_aspect('equal')
plt.savefig("out/fig%03d.png"%n,dpi=80)
结果:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)