0

I have 425 files, Ex1, Ex2, Ex3,...,Ex425. Each file correspond to one time-step. Each file contains one column of electric field amplitudes. So at time t=1, Ex1 is the field in space of 200 points, and so on. I want to make an one dimensional animation of these files in python but couldn't achieve it. Can anyone help?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation

basename = "Ex"
numFiles = 425

fig = plt.figure()
ax = plt.axes(xlim=(0, 200), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)

def init():
    line.set_data([], [])
    return line,

def animate(i):
    x = np.linspace(0, 200, 1)
    for t in range(1,numFiles):
        filename = basename + str(t) 
        data = np.loadtxt(filename)
    line.set_data(x, data[:,0])
    return line,

anim = animation.FuncAnimation(fig, animate, 
init_func=init,frames=200, interval=20, blit=True)

plt.show()
Jitendra
  • 103
  • 4
  • Animation with matplotlib are slightly more complex than this you will need the `animation` module. [Here is one of the few existing tutorial on the topic](https://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/). – snake_charmer Nov 04 '17 at 11:58
  • Or maybe interactive plotting would be useful for you... using `ion()` and `plt.draw()` instead of `plt.show()`. See [this](https://www.raspberrypi.org/learning/visualising-sorting-with-python/lesson-1/worksheet/) – snake_charmer Nov 04 '17 at 12:02
  • I saw those tutorials, but n0ne of them tell how to animate from importing data. More over, when I am reading data using data = np.loadtxt(filename), pythons makes an array, so it makes hard to plot. – Jitendra Nov 04 '17 at 12:09
  • Whether you are importing data from a file, a serial port or just generating random values should not change the general idea behind the second tutorial. If your issue is about formatting your arrays, you should change your question and make it about formatting instead. As for now, one cannot see the format of your files nor guess how you intend to structure your imports. – snake_charmer Nov 04 '17 at 12:21
  • I told about format of my files. Each file contain single column of 200 rows, and there are 425 such files corresponding to each time. – Jitendra Nov 04 '17 at 12:35

1 Answers1

0

Ok, first let's generate some files in the format I assumed was yours:

for i in range(11):
    a = open("test%i.txt"%i, "w")
    for j in np.random.rand(200)*i%5:
        a.write(str(j)+"\n")
    a.close()

Here is a way to plot their content dynamically:

## this function reads your files
# returns a list of strings
def read_files(filename):
    myfile = open(filename)
    file_content= myfile.readlines()
    myfile.close()
    return file_content

x = np.linspace(1, 200, 200)
y = np.ones(200) * np.nan

plt.ion() # enables figure updating

fig = plt.figure()
ax = fig.add_subplot(111)

## configure the plot
## these initial values for x and y will never be shown
line1, = ax.plot(x, y, 'b')  #     

## Loops through your files
for File in glob.glob("test*.txt"):
    y = read_files(File)
    line1.set_ydata(y)

    # recompute the ax.dataLim
    ax.relim()
    # update ax.viewLim using the new dataLim
    ax.autoscale_view()
    fig.canvas.draw() # display/update

    time.sleep(2)

Look at this answer for details regarding the dynamic scaling.

snake_charmer
  • 2,845
  • 4
  • 26
  • 39