Apr-17-2024, 11:24 AM
The example code below does not use pandas, it uses the data as .csv (which is text with commas, same as data in your first post)
It will plot all files in the data directory of the structure (tree) shown below:
Plot when 2nd element of data row = 1
It will plot all files in the data directory of the structure (tree) shown below:
Output:PlotData
├── data
│ └── csv
│ └── testdata.csv
├── src
│ └── PlotData.py
└──venv
# I use the following path tree for code as presented: # modify as you wish.Do # PlotData # ├── data # │ └── csv # │ └── testdata.csv # ├── src # │ └── PlotData.py # └──venv # # code named PlotData.py import os from matplotlib import pyplot as plt from pathlib import Path import csv class DataFormatter(): def __init__(self): os.chdir(os.path.abspath(os.path.dirname(__file__))) datapath = Path('../data') self.csvpath = datapath / 'csv' def plotdata(self, filename): # time row[0] is left axis # x0 = [] y0 = [] x1 = [] y1 = [] with filename.open() as fp: creader = csv.reader(fp) # plot for row[1] == 1 for n, row in enumerate(creader): if n == 0: header = row else: if row[1] == '1': x1.append(row[0]) y1.append(float(row[2])) else: x0.append(row[0]) y0.append(float(row[2])) self.plotxy(x0,y0,header) self.plotxy(x1,y1,header) def plotxy(self, x, y, header): plt.plot(x, y, color = 'g') plt.xlabel(header[0]) plt.ylabel(header[2]) plt.show() #--------------- End of class DataFormatter --------------- def App(): df = DataFormatter() filelist = [filename for filename in df.csvpath.iterdir() if filename.is_file() \ and filename.suffix == '.csv'] for file in filelist: df.plotdata(file) if __name__ == '__main__': App()Plot when 2nd element of data roe = 0
Plot when 2nd element of data row = 1