#scrollGraph import matplotlib.pyplot as plt import numpy as np import serial import random ESP32 = serial.Serial(port='COM8', baudrate=115200, timeout=.1) PAUSETIME = .01 # The smaller that this number gets, the less accurate it is. A good approximate time is 0.5 XMAX = 10 # Maximum X value for X axis x = [0] y = [0] MAXPOINTS = XMAX / PAUSETIME # Number of points visible on the graph visible_points = [0] fig, ax = plt.subplots() plt.title("Accelerometer", fontsize=20) plt.xlabel("Time") plt.ylabel("Acceleration") plt.xticks(np.arange(0, XMAX + 1, step=1)) ax.plot(x, y) ax.set_xlim(0, XMAX) i = 0 max_on_plot = 10 while True: acc_mag = ESP32.readline().decode('utf-8') st = len(acc_mag) if st != 0: #if True: stng = acc_mag[0:3] l = float(stng) #l = (10 * random.random() - i) #i += PAUSETIME x.append(x[-1] + PAUSETIME) y.append(l) if len(visible_points) <= MAXPOINTS: # If we haven't started scrolling yet: # print("havent scrolled") visible_points.append(l) else: # Once we have started scrolling: # print("started scrolling") visible_points.append(l) visible_points.pop(0) x.pop(0) max_on_plot = max(visible_points) ax.clear() plt.title("Accelerometer", fontsize=20) plt.xlabel("Time") plt.ylabel("Acceleration") plt.xticks(np.arange(0, x[-1] + 1, step=100)) #plt.xticks(visible=False) ax.plot(x, visible_points) ax.set_xlim(max(0, x[-1] - XMAX), x[-1]) ax.set_ylim(min(0, min(visible_points)), max_on_plot + 1) ax.figure.canvas.draw() plt.pause(PAUSETIME) plt.show()