I made a simple GUI using PyQt5 that plots encoder data from an Arduino in real time. I have some buttons in the GUI such as Start, Pause, and Exit. I have some others but those are not relevant. I am using matplotlib for the plotting of the data.
Not sure if this is helpful but these are my imported libraries etc…
import sys # Access system functions like sys.exit()
import serial # Read serial data from microcontroller
import numpy as np # Allows Scientific Calculations
from PyQt5 import QtCore, QtWidgets # Core Qt functions and GUI widgets
from PyQt5.QtWidgets import QMainWindow # Base class for main window
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas # Embed matplotlib in Qt
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure # Create matplotlib figures
from Gui_Test2 import Ui_Main_Window # Load GUI layout from Qt Designer
So the issue I am having is when I pause the data plotting. When I Pause I want to be able to scroll through the data history and zoom in/out etc… My Pause button works fine.
I have a variable called max_points that allows me to only view a certain number of data points at once on the graph. When I pause and go to scroll through all previous data, I am only able to see the data inside that window. All data outside that amount of points set by max_points is deleted or just not visible.
Here is the relevant code:
def update_plot(self):
try:
window_size = 10 # size of the moving average window
while self.ser.in_waiting:
self.serial_line = self.ser.readline().decode('utf-8').strip()
if self.serial_line:
try:
self.last_value = int(self.serial_line)
except ValueError:
continue
self.x_vals.append(self.i)
self.y_vals.append(self.last_value)
self.i += 1
# Plot most recent points
plot_x = self.x_vals[-self.max_points:]
plot_y = self.y_vals[-self.max_points:]
self.canvas.ax.clear()
if self.show_raw_data:
self.canvas.ax.plot(plot_x, plot_y, 'b-', label='Raw Data') # IF FIX DID NOT WORK REPLACE self.x_vals and self.y_vals back to plot_x and plot_y
# When I replace self.canvas.ax.plot(plot_x, plot_y, 'b-', label='Raw Data')
# with self.canvas.ax.plot(self.x_vals, self.y_vals, 'b-', label='Raw Data')
# it gets rid of the max_points viewing window
# This is not preferred as I do like the viewing window
So my goals are this:
- Still be able to see only a set amount of points at once
- Be able to pause the plotting and scroll through all previous data points that were plotted
Edit: I think it would be helpful to show what results I do get for better clarity of my problem:
Top Graph: Showcases the viewing window
Bottom Graph: Showcases the problem when I try to view previous data points