首页 > 解决方案 > 使用 matplotlib animate 和 patch 为正方形和箭头设置动画

问题描述

我想使用矩形和 jupter notebook 中的箭头为机器人的位置和速度设置动画。矩形块表示机器人在网格上的位置,箭头表示机器人的速度矢量。网格是一个矩阵,显示为 imshow 热图,其中可能的障碍物具有高热量。

现在,我有一个如下所示的代码

from matplotlib import animation
from matplotlib import patches
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(9,9))
ax = fig.add_subplot(1,1,1)

rectangle = patches.Rectangle((0,0), robo_width, robot_length,)
arrow = patches.Arrow(0,0,0,5)
img = plt.imshow(grid[0], cmap='hot', interpolation='nearest', animated=True)

def update(frame):

    rectangle.set_xy([x[frame], y[frame]])
    img.set_array(grid[frame])

    return img

animate = animation.FuncAnimation(fig, updatefig)

HTML(animate.to_jshtml())

有了这个,我可以在网格图像上绘制机器人正方形以显示机器人的位置。问题是,对于箭头补丁,我找不到类似于 set_xy() 的函数,也无法像矩形一样更新箭头。箭头有更新方法吗?还有另一种方法来绘制它吗?

标签: pythonpython-3.xmatplotlibanimation

解决方案


推荐阅读