0

I would like to make a video with some car traffic. For this I have all state information of all cars. Drawing the situation for a given time is no problem. Animation is. I made some code that looks like the code below but this does not work: nothing is moving. I do not understand the basics of animation. Can someone point me in the right direction?

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) 

Thank you.

updated: after writing, I clear the area. The full version works for me now.

frits
  • 327
  • 2
  • 15
  • http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ – tacaswell Jul 24 '14 at 14:49
  • 1
    http://stackoverflow.com/questions/4092927/generating-movie-from-python-without-saving-individual-frames-to-files/13983801#13983801 – tacaswell Jul 24 '14 at 14:49
  • http://stackoverflow.com/questions/19519587/python-matplotlib-plot-multi-lines-array-and-animation/19521738#19521738 – tacaswell Jul 24 '14 at 14:50
  • http://stackoverflow.com/questions/18745391/python-matplotlib-funcanimation-only-draws-one-frame/18746719#18746719 – tacaswell Jul 24 '14 at 14:50
  • After reading all comments I start to understand what is needed. I have a working program generating movie. Some things could be better (I think the images are too compressed, losing detail). But I have something to improve, not just emptiness. Thank you. – frits Jul 27 '14 at 06:55

2 Answers2

0

If you can generate images that show movement, you can just save them and create video using ffmpeg.

jure
  • 402
  • 5
  • 9
  • Than you for your suggestion. I thought of this but creating 10000+ images was not what I hoped for. It will be my backup plan. – frits Jul 24 '14 at 13:30
0

ax.cla() was a large part of the answer. Things can really improve but it works.

frits
  • 327
  • 2
  • 15