Browse Source
The code in this commit initialises the chart (sets labels legends...), grabs the latest reading from the light meters in Ritherdon and updates the chart (as an animation). A few things to note: 1. The code in this state only stores the latest 600 readings. This is usually just over one minutes worth of data (this might vary depending on computer and internet speed). 2. There are no time stamps on the x-axis (Time) because of I've made one chart plot and update 2 'streams' (in real-time). I'm going against the grain from what I understand of Matplotlib by doing this. All the documentation I've seen so far implies you want separate charts for different data streams/series. On top of that, updating one stream might mean the x-axis is out of sync. with the other stream. Because of this, I decided against trying to keep the x-axis updated with time-stamps. 3. The aim is to just show the readings as a chart.I have already made programs which communicate the status of the devices. This is mostly for when I'm not around the Windows tablet in the flat or running my Windows VM in the studio.master
Craig Oates
3 years ago
1 changed files with 59 additions and 0 deletions
@ -0,0 +1,59 @@
|
||||
import matplotlib.pyplot as plt |
||||
import matplotlib.animation as animation |
||||
import math |
||||
import requests |
||||
import datetime as dt |
||||
|
||||
# Persist HTTP connection (reduce requests/second). |
||||
session = requests.Session() |
||||
|
||||
# Parameters |
||||
x_length = 600 # No. of readings show at one time. |
||||
y_range = [-100, 100] # Light reading range. |
||||
|
||||
# Figure/Graph Details |
||||
fig = plt.figure() |
||||
ax = fig.add_subplot(1, 1, 1) |
||||
ys1 = [0] * x_length |
||||
ys2 = [0] * x_length |
||||
ax.set_ylim(y_range) |
||||
line1, = ax.plot(ys1, label='factory1') |
||||
line2, = ax.plot(ys2, label='factory2') |
||||
plt.title("Light Meter Readings in Ritherdon") |
||||
plt.xlabel("<--- Oldest (Time) Latest --->") |
||||
plt.ylabel("Light") |
||||
plt.legend() |
||||
plt.subplots_adjust(bottom=0.20) |
||||
ax.set_xticklabels(()) |
||||
|
||||
|
||||
# Updates the line chart for factory1 |
||||
def animate1(i, ys): |
||||
f1_request = session.get( |
||||
"http://ritherdon.abbether.net/api/readings/latest/1", timeout=5) |
||||
f1_data = f1_request.json() |
||||
f1_reading = f1_data.get("reading") |
||||
ys.append(f1_reading) |
||||
ys = ys1[-x_length:] |
||||
line1.set_ydata(ys) |
||||
return line1, |
||||
|
||||
|
||||
# Updates the line chart for factory2 |
||||
def animate2(i, ys): |
||||
f2_request = session.get( |
||||
"http://ritherdon.abbether.net/api/readings/latest/2", timeout=5) |
||||
f2_data = f2_request.json() |
||||
f2_reading = f2_data.get("reading") |
||||
ys.append(f2_reading) |
||||
ys = ys2[-x_length:] |
||||
line2.set_ydata(ys) |
||||
return line2, |
||||
|
||||
|
||||
# Starts the animations and shows the graph (I.E. 'main') |
||||
ani1 = animation.FuncAnimation( |
||||
fig, animate1, fargs=(ys1,), interval=100, blit=False) |
||||
ani2 = animation.FuncAnimation( |
||||
fig, animate2, fargs=(ys2,), interval=100, blit=False) |
||||
plt.show() |
Reference in new issue