您可以阅读scipy.misc import imread
背景图像和使用plt.imshow
动画中的背景来呈现。
下面的例子会生成一个圆圈(我们假设它的汽车),将“usa_map.jpg”放在背景中,然后在地图上移动圆圈。
奖金,你可以作为一个电影mp4
格式,采用使用编码器保存动画如ffmpeg
anim.save('the_movie.mp4', writer = 'ffmpeg', fps=30)
源代码
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.animation as animation
from scipy.misc import imread
img = imread("usa_map.jpg")
fig = plt.figure()
fig.set_dpi(100)
fig.set_size_inches(7, 6.5)
ax = plt.axes(xlim=(0, 20), ylim=(0, 20))
patch = plt.Circle((5, -5), 0.75, fc='y')
def init():
patch.center = (20, 20)
ax.add_patch(patch)
return patch,
def animate(i):
x, y = patch.center
x = 10 + 3 * np.sin(np.radians(i))
y = 10 + 3 * np.cos(np.radians(i))
patch.center = (x, y)
return patch,
anim = animation.FuncAnimation(fig, animate,
init_func=init,
frames=360,
interval=20,
blit=True)
plt.imshow(img,zorder=0, extent=[0.1, 20.0, 0.1, 20.0])
anim.save('the_movie.mp4', writer = 'ffmpeg', fps=30)
plt.show()
上面的代码会产生一个圆圈围绕美国移动animaton地图。它也将被保存为'the_movie.mp4',我不能在这里上传。
结果图像
感谢。它很好地工作 – narasimman
很高兴知道它解决了这个问题。投票表示赞赏。 –