0

So essentially I have a few numbered files like output_0000.dat ... output_1999.dat that each correspond to a consecutive timestep in the simulation of a wave on a string. The files contain the x and y positions for each point on the string at that timestep.

My goal is to plot these files one after another to make an animation, ideally using matplotlib but other software will also do.

If you could also direct me to a helpful tutorial, I'll be very greatful.

Thanks!

pnuts
  • 58,317
  • 11
  • 87
  • 139
Anna
  • 85
  • 8
  • Maybe a better question for http://stats.stackexchange.com/. The Cross validated forum, more data enthusiasts there! – CodePhobia Aug 18 '15 at 12:41
  • have you looked at the matplotlib examples? [here is a good one](http://matplotlib.org/1.4.2/examples/animation/basic_example.html) for your problem. [or another one](http://matplotlib.org/1.4.2/examples/animation/simple_anim.html) – tmdavison Aug 18 '15 at 12:41

1 Answers1

1

The animation tutorial suggested by @tom are probably the ideal solution, however the simplest way to do what you want is to use interactive plotting with filenames padded by zeros and numpy's genfromtxt,

import numpy as np
import matplotlib.pyplot as plt
plt.ion()
plt.show()

for i in range(2000):

    filename = "output_{0:04d}.dat".format(i)
    print("printing file = ", filename)
    xy = np.genfromtxt(filename)
    x = xy[:,0]; y = xy[:,1]
    plt.plot(x,y)
    plt.pause(0.01)

You may need to add delimiter to genfromtxt depending on the format of the data in your files.

Community
  • 1
  • 1
Ed Smith
  • 12,716
  • 2
  • 43
  • 55