I tried to write a class, that would receive some value, and then it would plot in. Graph would be continuous line. The problem, however, is that graph values are always for some reason always 1 entry behind.
from matplotlib import pyplot as plt
class RealTimeGraph():
x = []
y = []
def __init__(self, y_range = (), x_range = ()):
self.index = self.index_gen()
if (y_range):
plt.ylim(*y_range)
if (x_range):
plt.xlim(*x_range)
def index_gen(self, start = 0):
while True:
yield start
start += 1
def add_value(self, value):
self.x.append(next(self.index))
self.y.append(value)
plt.cla()
plt.plot(self.x, self.y)
plt.pause(0.005)
if __name__ == '__main__':
import random as r
import time
graph = RealTimeGraph(y_range = (0, 5))
for i in range(10):
graph.add_value(r.randint(0, 5))
time.sleep(0.5)
Sleep function is simulating program, in which I want to use this class. What's causing this issue?