Python 使用matplotlib设置流量动画

Python 使用matplotlib设置流量动画,python,animation,matplotlib,Python,Animation,Matplotlib,我想和一些汽车交通做一个视频。为此,我有所有汽车的所有状态信息。在给定的时间内描绘这种情况是没有问题的。动画是。我编写了一些代码,看起来像下面的代码,但这不起作用:没有任何东西在移动。我不懂动画的基本知识。有人能给我指出正确的方向吗 import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import matplotlib.animation as manimation FFMpegWriter = mani

我想和一些汽车交通做一个视频。为此,我有所有汽车的所有状态信息。在给定的时间内描绘这种情况是没有问题的。动画是。我编写了一些代码,看起来像下面的代码,但这不起作用:没有任何东西在移动。我不懂动画的基本知识。有人能给我指出正确的方向吗

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.animation as manimation
FFMpegWriter = manimation.writers['ffmpeg']
writer = FFMpegWriter(fps=10)

def animate_traffic():
    fig=plt.figure(1)
    ax=fig.add_subplot(1,1,1)
    tsim=tstart
    with writer.saving(fig, "roadtest.mp4", 100):
        for i in range(100):
            draw_roadlayout()
            for car in cars:
                # draw each of the cars on the road
                # based on time tsim
            plt.grid(False)
            ax.axis(plt_area)
            fig   = plt.gcf()
            writer.grab_frame()
            ax.cla()
            tsim+=timestep
    plt.close(1) 
多谢各位


更新:写完后,我清理了这个区域。完整版本现在适合我。

如果你能生成显示运动的图像,你只需保存它们并使用ffmpeg创建视频。

ax.cla是答案的一大部分。事情确实可以改善,但它是有效的。

谢谢你的建议。我想到了这一点,但创造10000多张图片并不是我所希望的。这将是我的备份计划。在阅读了所有评论后,我开始了解需要什么。我有一个生成电影的工作程序。有些东西可能会更好,我认为图像太过压缩,失去了细节。但我还有一些需要改进的地方,不仅仅是空虚。非常感谢。