Files
voltage/plot.py

46 lines
1.2 KiB
Python

import os
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
from datetime import datetime
DATA_DIR = "data"
# Find the latest data file
files = [f for f in os.listdir(DATA_DIR) if f.startswith("voltages_") and f.endswith(".csv")]
if not files:
raise FileNotFoundError("No voltage data files found in 'data/'")
latest_file = max(files, key=lambda f: os.path.getmtime(os.path.join(DATA_DIR, f)))
filepath = os.path.join(DATA_DIR, latest_file)
print(f"Plotting data from {filepath}")
# Load CSV data
timestamps = []
voltages = []
with open(filepath, "r") as f:
next(f) # skip header
for line in f:
ts_str, v_str = line.strip().split(",")
ts = datetime.strptime(ts_str, "%Y-%m-%d %H:%M:%S.%f")
timestamps.append(ts)
voltages.append(float(v_str))
# Plot
plt.figure(figsize=(12, 5))
plt.scatter(timestamps, voltages, marker='o', linestyle='-', s=0.1)
plt.xlabel("Time (CET)")
plt.ylabel("Voltage (V)")
plt.title(f"Voltage Measurements ({latest_file})")
plt.grid(True)
# Format x-axis for readable time
plt.gcf().autofmt_xdate()
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%H:%M:%S"))
plt.tight_layout()
plt.show()