96 lines
1.8 KiB
Python
96 lines
1.8 KiB
Python
from collections import deque
|
|
import numpy as np
|
|
from matplotlib import pyplot as plt
|
|
|
|
|
|
|
|
class Cache:
|
|
|
|
def __init__(self, size=100):
|
|
self.data = deque(maxlen=size)
|
|
|
|
def append(self, value):
|
|
self.data.append(value)
|
|
|
|
@property
|
|
def x(self):
|
|
length = len(self.data)
|
|
return np.arange(length)
|
|
|
|
@property
|
|
def y(self):
|
|
return np.array(self.data)
|
|
|
|
@property
|
|
def xy(self):
|
|
return self.x, self.y
|
|
|
|
|
|
|
|
def hist(values, bins="auto"):
|
|
ys, xs = np.histogram(values, bins="auto")
|
|
width = (xs[1:] - xs[:-1]).mean()
|
|
xs = xs[:-1] # remove closing edge
|
|
length = len(xs)
|
|
return xs, ys, width, length
|
|
|
|
|
|
|
|
|
|
class Plot0D:
|
|
|
|
def __init__(self, num, *args, **kwargs):
|
|
self.fig, axs = plt.subplots(1, 2)
|
|
self.ax_time, self.ax_hist = ax_time, ax_hist = axs
|
|
|
|
print("cache length", num or "infinite")
|
|
self.cache = Cache(num)
|
|
|
|
lines = ax_time.plot([0], [0])
|
|
self.plot_time = lines[0] #TODO: wtf?
|
|
|
|
self.color = None # this will store the color after the first hist
|
|
|
|
|
|
|
|
def set(self, value):
|
|
cache = self.cache
|
|
cache.append(value)
|
|
x, y = cache.xy
|
|
|
|
|
|
self.plot_time.set_data(x, y)
|
|
|
|
ax_time = self.ax_time
|
|
|
|
ymin, ymax = min(y), max(y)
|
|
if ymin != ymax:
|
|
ax_time.set_ylim(ymin, ymax)
|
|
|
|
ax_time.relim()
|
|
ax_time.autoscale_view()
|
|
|
|
|
|
ax_hist = self.ax_hist
|
|
|
|
try:
|
|
self.plot_hist.remove()
|
|
except:
|
|
pass
|
|
|
|
xs, ys, width, length = hist(y)
|
|
|
|
plot_hist = ax_hist.bar(xs, ys, align="edge", width=width, color=self.color)
|
|
self.plot_hist = plot_hist
|
|
|
|
self.color = plot_hist.patches[0].get_facecolor()
|
|
|
|
ax_hist.set_title(f"#bins: {length}")
|
|
|
|
ax_hist.relim()
|
|
ax_hist.autoscale_view()
|
|
|
|
|
|
|
|
|