Improved plotter script

This commit is contained in:
Maximilian Giller 2024-05-03 22:39:30 +02:00
parent 2fb05d641b
commit c84b53a374

View file

@ -1,19 +1,31 @@
import requests import requests
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime from datetime import datetime
import json
file_path = "history.json"
history_url = "http://192.168.178.84:9587/bettwaage/history" 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 # make data
x = [datetime.strptime(d["timestamp"], "%Y-%m-%d %H:%M:%S.%f") for d in data] x = [d["timestamp"] for d in data]
max_time = max(x) x = [datetime.strptime(d, "%Y-%m-%d %H:%M:%S.%f") for d in x]
x = [(max_time - d).total_seconds() 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] total = [d["total"] for d in data]
tl = [d["tl"] 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() fig, ax1 = plt.subplots()
color = "tab:blue" ax1.set_xlabel("Time (s)")
ax1.set_xlabel("time (s)") ax1.set_ylabel("Weight (kg)")
ax1.set_ylabel("Total", color=color)
ax1.plot(x, total, color=color)
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" ax1.legend(["Total", "Top Left", "Top Right", "Bottom Left", "Bottom Right"])
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")
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.show() plt.show()