Slow
Slow

Reputation: 3

Apply my code for multiples files with same data

I wrote a code to draw the masse center from X and Y coordinate, taking it from a file cell1.txt. But I want now to make my code works for many files cell2.txt cell3.tx cell4.txt... How Can I make it loops for all my data files and plot my data.

import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.animation import FFMpegWriter
import matplotlib.pyplot as plt
import numpy as np
# import polygon as poly
x, y = np.loadtxt('cell1.txt', delimiter='  ', unpack=True)
    


bounds = np.argwhere(np.diff(x) < 0).squeeze() # find the boundaries
bounds = np.concatenate([[0], bounds + 1, [len(x)]]) # additional boundaries for the first and last point
for b0, b1 in zip(bounds[:-1], bounds[1:]):
    plt.plot(x[b0:b1], y[b0:b1],'r') # use '-ro' for only red curves
    plt.plot(xbon, ybon, 'o' ,color = 'k' , markersize=0.8)
    plt.axis('equal')
    plt.axis('on')
    title1 = ' Trajectories '
    title2 = '30%'
    plt.title(title1+title2, fontsize=16, fontweight='bold')
plt.show()

Upvotes: 0

Views: 35

Answers (1)

Karina
Karina

Reputation: 1280

you need

import os
list_of_files = os.listdir()

the variable list_of_files is the list of all your file in the same folder of your python file, if you want to have another folder, insert your path in the brackets. If you have the list of all the files you have, you can just loop it. The loop will start at the top (directly under list_of_files = os.listdir()) and end just before plt.show. Something like this would do:

for i in range(len(list_of_files)):
     if list_of_files[i][0:4] == 'cell':
         # paste your code here

PS: I did not and can not try your code. I assume according to your description that it is working just fine, and you only need to loop over other files.

side note: you should have added python in your tags.

Upvotes: 1

Related Questions