2017-09-22 30 views
0

我想在车辆移动时在背景中显示地图。我正在使用matplotlib动画功能。运动看起来很好。但我在加载地图时尝试了以下内容。地图未加载。只有黑色补丁可见。我也试着指定zorder。但没有用。在matplotlib动画背景中显示地图

ani = animation.FuncAnimation(fig, animate, len(x11),interval=150, 
          blit=True, init_func=init, repeat=False) 

img = cbook.get_sample_data('..\\maps.png') 
image = plt.imread(img) 
plt.imshow(image) 
plt.show() 

回答

1

您可以阅读scipy.misc import imread背景图像和使用plt.imshow动画中的背景来呈现。

下面的例子会生成一个圆圈(我们假设它的汽车),将“usa_map.jpg”放在背景中,然后在地图上移动圆圈。

奖金,你可以作为一个电影mp4格式,采用使用编码器保存动画如ffmpeganim.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',我不能在这里上传。

结果图像
enter image description here

+0

感谢。它很好地工作 – narasimman

+0

很高兴知道它解决了这个问题。投票表示赞赏。 –