From 6f712bbe9fb95198442072834a4ae5ad2c8336cd Mon Sep 17 00:00:00 2001
From: Samuel <sa_schmi@rhrk.uni-kl.de>
Date: Fri, 14 Jul 2023 14:58:44 +0200
Subject: [PATCH] date on x axis

---
 src/humidity/read.py | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)

diff --git a/src/humidity/read.py b/src/humidity/read.py
index e7f9e78..f64f649 100644
--- a/src/humidity/read.py
+++ b/src/humidity/read.py
@@ -1,6 +1,7 @@
 import datetime
 import matplotlib.pyplot as plt
 import argparse
+import matplotlib.dates as mdates
 
 parser = argparse.ArgumentParser(description='Parse inpath.')
 parser.add_argument('--inpath', dest='inpath', required=True)
@@ -17,17 +18,25 @@ humiditys = []
 
 with open(inpath, 'r') as myfile:
     for x in myfile:
-        data = x.split(",")
-        date = data[0]
-        temperature = round(float(data[1]), 2)
-        humidity = round(float(data[2]), 2)
+        if "Error" not in x:
+            data = x.split(",")
+            date = data[0]
+            temperature = round(float(data[1]), 2)
+            humidity = round(float(data[2]), 2)
+            
+            dates.append(datetime.datetime.strptime(date, "%d_%m_%Y_%H_%M_%S_%f"))
+            temperatures.append(temperature)
+            humiditys.append(humidity)
         
-        dates.append(datetime.datetime.strptime(date, "%d_%m_%Y_%H_%M_%S_%f"))
-        temperatures.append(temperature)
-        humiditys.append(humidity)
-        
-timestamps = [(dates[i]-dates[0]).total_seconds() for i in range(len(dates))]
-plt.plot(timestamps, temperatures)
-plt.plot(timestamps, humiditys)
+fig, ax = plt.subplots()
+ax.plot(dates, temperatures)
+ax.plot(dates, humiditys)
+
+ax.xaxis.set_major_locator(mdates.MinuteLocator(byminute=range(0,60,2)))
+ax.xaxis.set_minor_locator(mdates.SecondLocator(bysecond=range(0,60,30)))
+ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
+
+fig.autofmt_xdate()
+
 plt.legend(["Temperatur", "Relative Luftfeuchte"])
 plt.show()
\ No newline at end of file
-- 
GitLab