-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotter.py
83 lines (62 loc) · 3.11 KB
/
plotter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 18 14:41:30 2024
@author: laqm
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
class Plotter:
def __init__(self, canvas, ax, data_handler):
self.canvas = canvas
self.ax = ax
self.autoscale = True
self.plot_type = "line_graph"
self.data_handler = data_handler
self.x_option = 'Time'
self.y_option = 'Channel1(X)'
# Occurs every two seconds
def update_plot(self, data, x_option, y_option):
self.ax.autoscale(self.autoscale)
if self.plot_type == "line_graph":
self.ax.plot(data[x_option], data[y_option], color="blue")
elif self.plot_type == "scatter_plot":
self.ax.scatter(data[x_option], data[y_option], color="blue")
self.ax.set_xlabel(x_option)
self.ax.set_ylabel(y_option)
self.x_option = x_option
self.y_option = y_option
self.ax.set_title(f'{y_option} vs. {x_option}')
self.canvas.draw()
# note* randomly breaking, need to fix.
# highlights and annotates the nearest data point
def on_mouse_move(self, event):
if event.inaxes is not None and self.data_handler.data['Time']:
mouse_x = event.xdata
mouse_y = event.ydata
distances = np.sqrt((np.array(self.data_handler.data[self.x_option]) - mouse_x) ** 2 +
(np.array(self.data_handler.data[self.y_option]) - mouse_y) ** 2)
nearest_index = np.argmin(distances)
xlim, ylim = self.ax.get_xlim(), self.ax.get_ylim()
self.ax.cla()
if not self.autoscale:
self.ax.set_xlim(xlim)
self.ax.set_ylim(ylim)
self.ax.autoscale(self.autoscale)
if self.plot_type == "line_graph":
self.ax.plot(self.data_handler.data[self.x_option], self.data_handler.data[self.y_option], color="blue")
elif self.plot_type == "scatter_plot":
self.ax.scatter(self.data_handler.data[self.x_option], self.data_handler.data[self.y_option], color="blue")
self.ax.plot(self.data_handler.data[self.x_option][nearest_index],
self.data_handler.data[self.y_option][nearest_index], 'ro')
self.ax.annotate(f'({self.data_handler.data[self.x_option][nearest_index]}, '
f'{self.data_handler.data[self.y_option][nearest_index]})',
(self.data_handler.data[self.x_option][nearest_index],
self.data_handler.data[self.y_option][nearest_index]),
textcoords="offset points", xytext=(0, 10), ha='center', fontsize=8, color='red')
self.ax.set_xlabel(self.x_option)
self.ax.set_ylabel(self.y_option)
self.ax.set_title(f'{self.y_option} vs. {self.x_option}')
self.canvas.draw()
def toggle_autoscale(self):
self.autoscale = not self.autoscale