首页 > 解决方案 > 带有动画的图像上的 Python 绘图

问题描述

我想为每个新位置连续绘制点,i同时清除以前的帧以可视化运动。我在正确的轨道上吗?我习惯于使用该setinterval函数在 Javascript 中对此进行编码,但我是 Python 新手。

fig = plt.figure(1)
    ax  = fig.add_subplot(111)
    ax.imshow(img, cmap=plt.cm.gray)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_xlim(0,img.shape[1])
    ax.plot(np.r_[x,x[0]], np.r_[y,y[0]], c=(0.5,0.5,0.5), lw=1)

    for i, snake in enumerate(snakePoints):
        if i % 15 == 0:
            ax.plot(np.r_[snake[0], snake[0][0]], np.r_[snake[1], snake[1][0]], c=(0.1,0,1,0.3), lw=1)


    ax.plot(np.r_[snakePoints[-1][0], snakePoints[-1][0][0]], np.r_[snakePoints[-1][1], snakePoints[-1][1][0]], c=(1,0,0), lw=3)
    plt.show()

标签: pythonanimationsetinterval

解决方案


尝试在下面使用

ax.clear() # to clear the previous output

plt.pause(0.001) # to pause and show animation for new plot 

并编写一个函数来调用新参数和绘图。我想你的缩进是错误的。


推荐阅读