Python 3.x 使用matplotlib动画在背景中显示地图
我想在车辆移动时在背景中显示地图。我正在使用matplotlib动画功能。运动看起来不错。但我在加载地图时尝试了以下方法。地图没有加载。只有一个黑色补丁可见。我也试图指定zorder。但什么都不管用Python 3.x 使用matplotlib动画在背景中显示地图,python-3.x,matplotlib,Python 3.x,Matplotlib,我想在车辆移动时在背景中显示地图。我正在使用matplotlib动画功能。运动看起来不错。但我在加载地图时尝试了以下方法。地图没有加载。只有一个黑色补丁可见。我也试图指定zorder。但什么都不管用 ani = animation.FuncAnimation(fig, animate, len(x11),interval=150, blit=True, init_func=init, repeat=False) img = cbook.get
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()
您可以使用
scipy.misc import imread
读取背景图像,并使用plt.imshow
在动画背景中渲染
下面的示例生成一个圆圈(我们假设它是您的车),将“usa_map.jpg”置于背景中,然后将圆圈移动到地图上
另外,您可以使用编码器(如ffmpeg
)将动画保存为mp4
格式的电影,使用anim.save('the_movie.mp4',writer='ffmpeg',fps=30)
源代码
import numpy as np
import matplotlib.pyplot as plt
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 = 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()
上面的代码将生成一个围绕美国地图移动的圆圈的动画。它也将被保存为'theu movie.mp4',我不能在这里上传
结果图像您可以使用
scipy.misc import imread
读取背景图像,并使用plt.imshow
在动画背景中渲染
下面的示例生成一个圆圈(我们假设它是您的车),将“usa_map.jpg”置于背景中,然后将圆圈移动到地图上
另外,您可以使用编码器(如ffmpeg
)将动画保存为mp4
格式的电影,使用anim.save('the_movie.mp4',writer='ffmpeg',fps=30)
源代码
import numpy as np
import matplotlib.pyplot as plt
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 = 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()
上面的代码将生成一个围绕美国地图移动的圆圈的动画。它也将被保存为'theu movie.mp4',我不能在这里上传
结果图像