Repository navigation
Expand file tree
/
Copy pathplot.py
More file actions
95 lines (79 loc) · 4.01 KB
/
Copy pathplot.py
File metadata and controls
95 lines (79 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates # Importieren für Datumformatierung
import io # Nur für dieses Beispiel, um Daten als String zu simulieren
# --- SCHRITT 1: Die Datei 'log.csv' lesen ---
# Stellen Sie sicher, dass 'log.csv' im selben Ordner wie Ihr Python-Skript ist.
try:
df = pd.read_csv('log.csv')
print("Die Datei 'log.csv' wurde erfolgreich gelesen.")
print("Erste 5 Zeilen der Daten:")
print(df.head().to_markdown(index=False, numalign="left", stralign="left"))
# Determine the timestamp column name dynamically or use the correct one based on your file
# If your log.csv has 'timestamp', use that. If it has 'timestamp_utc', use that.
TIMESTAMP_COLUMN = 'timestamp' # <--- CHANGED THIS LINE based on your screenshot
if TIMESTAMP_COLUMN not in df.columns:
print(f"Warnung: Die Spalte '{TIMESTAMP_COLUMN}' wurde nicht gefunden. Versuche 'timestamp_utc'.")
TIMESTAMP_COLUMN = 'timestamp_utc'
if TIMESTAMP_COLUMN not in df.columns:
print("Fehler: Weder 'timestamp' noch 'timestamp_utc' Spalte gefunden. Bitte überprüfen Sie Ihr log.csv.")
raise KeyError("Timestamp column not found.")
except FileNotFoundError:
print("Fehler: 'log.csv' wurde nicht gefunden. Stellen Sie sicher, dass die Datei im selben Ordner ist.")
print("Verwende stattdessen Beispieldaten, um den Plot zu demonstrieren.")
# --- SIMULIERTE DATEN (FALLS log.csv NICHT GEFUNDEN WIRD) ---
csv_data = """timestamp_utc,temperature_c,humidity_pct
2025-07-18T00:18:55Z,28.70,41.90
2025-07-18T00:18:58Z,28.60,44.40
2025-07-18T00:19:00Z,28.70,44.70
2025-07-18T00:19:03Z,28.70,45.10
2025-07-18T00:19:05Z,28.70,45.10
2025-07-18 00:23:16,29.00,42.50
2025-07-18T00:23:55Z,29.20,42.90
2025-07-18T00:24:55Z,29.30,43.10
2025-07-18T00:25:55Z,29.40,43.30
2025-07-18T00:26:55Z,29.50,43.50
2025-07-18T00:27:55Z,29.60,43.70
2025-07-18T00:28:55Z,29.70,43.90
2025-07-18T00:29:55Z,29.80,44.10
"""
df = pd.read_csv(io.StringIO(csv_data))
TIMESTAMP_COLUMN = 'timestamp_utc' # For simulated data, it's 'timestamp_utc'
# --- SCHRITT 2: Daten vorbereiten ---
# Use the determined TIMESTAMP_COLUMN
df[TIMESTAMP_COLUMN] = pd.to_datetime(df[TIMESTAMP_COLUMN], errors='coerce')
df.dropna(subset=[TIMESTAMP_COLUMN], inplace=True)
print("\nDataFrame nach dem Entfernen von Zeilen mit ungültigen Zeitstempeln:")
print(df.head().to_markdown(index=False, numalign="left", stralign="left"))
# --- SCHRITT 3: Graph erstellen (nur Matplotlib) ---
fig, ax1 = plt.subplots(figsize=(12, 7))
# Plot Temperatur auf ax1
color_temp = 'tab:red'
ax1.set_xlabel('Time (UTC)') # Label still refers to UTC as per Arduino code intention
ax1.set_ylabel('Temperature (°C)', color=color_temp)
# Marker von 'o' auf 'x' geändert
ax1.plot(df[TIMESTAMP_COLUMN], df['temperature_c'], color=color_temp, marker='x', label='Temperature (°C)')
ax1.tick_params(axis='y', labelcolor=color_temp)
ax1.grid(True, which='both', linestyle='--', linewidth=0.5)
# Erstelle eine zweite Y-Achse für die Luftfeuchtigkeit
ax2 = ax1.twinx()
color_humidity = 'tab:blue'
ax2.set_ylabel('Humidity (%)', color=color_humidity)
# Marker von 'x' auf 'x' (war vorher 'o' im Beisp.) geändert
ax2.plot(df[TIMESTAMP_COLUMN], df['humidity_pct'], color=color_humidity, marker='x', label='Humidity (%)')
ax2.tick_params(axis='y', labelcolor=color_humidity)
# Titel des Graphen
plt.title('Temperature and Humidity Over Time')
# --- NEU: X-Achsen-Formatierung für Datum und Uhrzeit ---
# Definieren des Formats: Jahr-Monat-Tag Stunde:Minute:Sekunde
date_form = mdates.DateFormatter('%Y-%m-%d %H:%M:%S')
ax1.xaxis.set_major_formatter(date_form)
# X-Achsen-Beschriftungen rotieren für bessere Lesbarkeit
fig.autofmt_xdate()
# Legenden für beide Achsen kombinieren und anzeigen
lines1, labels1 = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines1 + lines2, labels1 + labels2, loc='upper left')
plt.tight_layout()
plt.show()
# plt.show() # No need for two plt.show() calls unless you want two separate windows.