我正在尝试使用Matplotlib的FuncAnimation
来动画显示每帧动画中的一个点。
# modules
#------------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as py
from matplotlib import animation
py.close('all') # close all previous plots
# create a random line to plot
#------------------------------------------------------------------------------
x = np.random.rand(40)
y = np.random.rand(40)
py.figure(1)
py.scatter(x, y, s=60)
py.axis([0, 1, 0, 1])
py.show()
# animation of a scatter plot using x, y from above
#------------------------------------------------------------------------------
fig = py.figure(2)
ax = py.axes(xlim=(0, 1), ylim=(0, 1))
scat = ax.scatter([], [], s=60)
def init():
scat.set_offsets([])
return scat,
def animate(i):
scat.set_offsets([x[:i], y[:i]])
return scat,
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=len(x)+1,
interval=200, blit=False, repeat=False)
不幸的是,最终的动画情节与原始情节不同。动画绘图还会在动画的每一帧中闪烁几个点。关于如何使用animation
包正确地制作散点图动画,有什么建议吗?
发布于 2018-11-22 01:29:00
免责声明,我写了一个名为celluloid的库,试图使用ArtistAnimation
来简化这一过程。你基本上可以像往常一样编写可视化代码,只需在绘制完每一帧后拍摄照片。下面是一个完整的示例:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
from celluloid import Camera
fig = plt.figure()
camera = Camera(fig)
dots = 40
X, Y = np.random.rand(2, dots)
plt.xlim(X.min(), X.max())
plt.ylim(Y.min(), Y.max())
for x, y in zip(X, Y):
plt.scatter(x, y)
camera.snap()
anim = camera.animate(blit=True)
anim.save('dots.gif', writer='imagemagick')
https://stackoverflow.com/questions/26892392
复制相似问题