【问题标题】:animate traffic using matplotlib使用 matplotlib 动画交通
【发布时间】:2014-09-15 16:42:40
【问题描述】:

我想制作一段包含一些汽车交通的视频。为此,我拥有所有汽车的所有状态信息。绘制给定时间的情况是没有问题的。动画是。我编写了一些看起来像下面的代码的代码,但这不起作用:什么都没有移动。我不了解动画的基础知识。有人能指出我正确的方向吗?

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) 

谢谢。

更新:写完后,我清除了该区域。完整版现在适合我。

【问题讨论】:

标签: python animation matplotlib


【解决方案1】:

如果您可以生成显示运动的图像,则可以将它们保存并使用 ffmpeg 创建视频。

【讨论】:

  • 感谢您的建议。我想到了这一点,但创建 10000 多张图像并不是我所希望的。这将是我的后备计划。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 2016-11-02
    • 2015-07-02
    相关资源
    最近更新 更多