【发布时间】:2017-08-21 20:07:11
【问题描述】:
虽然数小时的 stackoverflow 浏览确实改进了我的 python 动画代码,但我无法完全弄清楚一件事,因此我转向社区的善良灵魂,希望有人能够解开一些谜团。
简而言之,我有一个大约 2000 x 1000 像素的背景图像,比方说,我需要在该图像上散布一些点并对过程进行动画处理并将整个图像保存为视频。我只能根据需要更新散点图,但我无法删除先前的散点图。所以输出不是我真正想要的。如果有人可以看一下代码并查看故障所在,我会很高兴。我用过 scat.remove() 似乎什么也没做。
提前谢谢各位。
import matplotlib.pyplot as plt
import pylab as pl
import numpy as np
from pylab import savefig
from matplotlib import animation
import matplotlib
######################################################
fig = plt.figure()
ax = plt.axes()
a = plt.imread('background.jpg')
im = plt.imshow(a)
#######################################################
def randpair(n):
x,y=[],[]
for i in xrange(n):
x.append(np.random.randint(100,1900))
y.append(np.random.randint(100,900))
return x,y
def animate(i):
scat = ax.scatter(0,0,color='white')
points = np.random.randint(5,size=10)
for j in points:
xy = randpair(j)
x = xy[0]
y = xy[1]
print x,y
if len(x) > 0 :
scat.remove()
scat = ax.scatter(x,y,color='r',s=18)
plt.xticks([])
plt.yticks([])
return scat,ax, # ax returns the text to be updated and scat returns the scatterplot.
anim = animation.FuncAnimation(fig, animate, 49,interval=300, blit=True)
writer = animation.writers['ffmpeg']
anim.save('film_3.mp4')
#plt.show()
【问题讨论】:
标签: python animation matplotlib scatter-plot