From c84b53a374c2149f617e62cdf5ddc019b5f8a6a8 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Fri, 3 May 2024 22:39:30 +0200 Subject: [PATCH] Improved plotter script --- bettwaage-plotter/main.py | 44 +++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/bettwaage-plotter/main.py b/bettwaage-plotter/main.py index afc6dde..50a3a36 100644 --- a/bettwaage-plotter/main.py +++ b/bettwaage-plotter/main.py @@ -1,19 +1,31 @@ import requests import matplotlib.pyplot as plt -import numpy as np from datetime import datetime +import json + +file_path = "history.json" history_url = "http://192.168.178.84:9587/bettwaage/history" -data = requests.get(history_url) +convert_time_to_seconds = True -data = data.json() +# Script +data = None +if file_path is None: + data = requests.get(history_url) + data = data.json() +else: + with open(file_path, "r") as fp: + data = json.load(fp) # make data -x = [datetime.strptime(d["timestamp"], "%Y-%m-%d %H:%M:%S.%f") for d in data] -max_time = max(x) -x = [(max_time - d).total_seconds() for d in x] +x = [d["timestamp"] for d in data] +x = [datetime.strptime(d, "%Y-%m-%d %H:%M:%S.%f") for d in x] + +if convert_time_to_seconds: + max_time = max(x) + x = [(d - max_time).total_seconds() for d in x] total = [d["total"] for d in data] tl = [d["tl"] for d in data] @@ -24,19 +36,15 @@ br = [d["br"] for d in data] fig, ax1 = plt.subplots() -color = "tab:blue" -ax1.set_xlabel("time (s)") -ax1.set_ylabel("Total", color=color) -ax1.plot(x, total, color=color) +ax1.set_xlabel("Time (s)") +ax1.set_ylabel("Weight (kg)") -ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis +line_total = ax1.plot(x, total, color="tab:blue") +line_tl = ax1.plot(x, tl, color="tab:red") +line_tr = ax1.plot(x, tr, color="tab:green") +line_bl = ax1.plot(x, bl, color="tab:orange") +line_br = ax1.plot(x, br, color="tab:purple") -color = "tab:blue" -ax2.set_ylabel("Legs", color=color) # we already handled the x-label with ax1 -ax2.plot(x, tl, color="tab:red") -ax2.plot(x, tr, color="tab:green") -ax2.plot(x, bl, color="tab:purple") -ax2.plot(x, br, color="tab:orange") +ax1.legend(["Total", "Top Left", "Top Right", "Bottom Left", "Bottom Right"]) -fig.tight_layout() # otherwise the right y-label is slightly clipped plt.show()