0

I am trying to write a script that will analyze the performance of a machine based on operation time (time vs power). this means more and more csv files will be generated and will be added to the plot.

all the csv's are in the same folder. my code is:

import pandas as pd
import os
import matplotlib.pyplot as plt
import numpy
from datetime import datetime
from pathlib import Path
import pandas as pd

dateparse = lambda x: datetime.strptime(x, '%d-%b-%Y %I:%M:%S:%f %p')
folder = r"path adrress"
files = Path(folder).rglob('*.csv')
result_path = r"path adrress"

fig = plt.figure(figsize=(60, 20))
plt.grid(True)
plt.ylim(bottom=43.5, top=44.5)
plt.xlabel("Time (s)", size=50)
plt.ylabel("Power (dBm)", size=50)
color = ['black', 'red', 'green', 'orange', 'blue', 'purple']
label = ['504', '505', '506']
for i, file in enumerate(files):
    df_files = (pd.read_csv(file, skiprows=7, parse_dates=['Time Stamp'], date_parser=dateparse) for file in files)
    df = pd.concat(df_files)
    x = df['Time Stamp']
    y = df[' Power (dBm)'].to_numpy()
    plt.scatter(x, y, color=color[i], label=label[i], linewidth=4)
plt.legend(loc=3, prop={'size': 30}, markerscale=5)
plt.xticks(color='k', size=50)
plt.yticks(color='k', size=50)
os.makedirs(result_path, exist_ok=True)
imageFileName = os.path.join(result_path, 'plot.png')
if os.path.isfile(imageFileName):
    os.remove(imageFileName)
plt.savefig(imageFileName)
plt.close(fig)

the code creates a single plot in a single color with a single instance in the legend instead of 3 (currently 3 files in the folder).

this is the figure created

How can I reflect the different files in the figure ?

Derlin
  • 9,572
  • 2
  • 32
  • 53
  • Hey Omer, I think what you want is called `subplots`, which will let you get a separate graph for each file in your figure. You can set colours specifically if that's what you're after too by using each axis object. [This](https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html) is a good example of how to get it done. – Dan White Aug 19 '21 at 13:42
  • This [answer](https://stackoverflow.com/a/66052914/7758804) is the easiest way to create and plot in subplots. – Trenton McKinney Aug 19 '21 at 14:14
  • Try to remove concat, use only df = pd.read_csv(file, skiprows=7, parse_dates=['Time Stamp'], date_parser=dateparse) – ffsedd Aug 19 '21 at 14:15
  • for subplots, ill have to enter each plot separately, it's good for small numbers but eventually i'll have a lot of csv's. – Omer Davidi Aug 19 '21 at 17:11
  • also, tried without the concat, it's just overwrite the previous csv's and just plots the last one. – Omer Davidi Aug 19 '21 at 17:12

0 Answers0