-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlotGraph.py
More file actions
193 lines (145 loc) · 8.25 KB
/
PlotGraph.py
File metadata and controls
193 lines (145 loc) · 8.25 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
from matplotlib.widgets import Button
from matplotlib.widgets import CheckButtons
from matplotlib.widgets import Slider
from tkinter import filedialog
style.use('seaborn-whitegrid')
fig = plt.figure()
ax = plt.axes()
ax.grid(which='major', linewidth='0.5', color='black', alpha=1) # Customize the major grid
ax.grid(which='minor', linestyle=':', linewidth='0.5', color='black', alpha=1) # Customize the minor grid
class makeGraph:
def __init__(self, shared_data_supervisor, shared_data_time, shared_data_head, shared_data_pitch, shared_data_roll):
self.shared_data_supervisor = shared_data_supervisor
self.shared_data_time = shared_data_time
self.shared_data_head = shared_data_head
self.shared_data_pitch = shared_data_pitch
self.shared_data_roll = shared_data_roll
self.lines_visibility = [True, True,
True] # it is a self variable for regulating visibility of lines. initializing all the lines to visible
self.supervisor = 'stop' # it is a self variable for regulating start, stop and clearing of the lines
self.gridregulator = False # it is a self variable for regulating minor grids
# initializing the slider_x_value
self.slider_x_value = 0
def animate(self, i):
self.shared_data_supervisor[0] = self.supervisor
ax.minorticks_on() if self.gridregulator else ax.minorticks_off() # turning on and off the minorticks(minorgrids) according to the value of self.gridregulator
if self.supervisor == 'stop':
return # if the stop button is pressed then the animate function returns without updating the axis
if self.supervisor == 'clear':
ax.clear()
return # if the clear button is pressed then the animate function return by clearing the datas
self.length_x_vals = len(
self.shared_data_time) # storing the length of self variable x_vals to another self variable length_x_vals for further use
ax.clear() # clearing the axes before updating
ax.minorticks_on() if self.gridregulator else ax.minorticks_off() # turning on and off the minorticks(minorgrids) according to the value of self.gridregulator after the axes is cleared
# plotting the lines into the axes and giving each line a name
lhead, = ax.plot(self.shared_data_time[0:self.length_x_vals], self.shared_data_head[0:self.length_x_vals],
'-bo', lw=1, label='Head', visible=self.lines_visibility[0])
lpitch, = ax.plot(self.shared_data_time[0:self.length_x_vals], self.shared_data_pitch[0:self.length_x_vals],
'-go', lw=1, label='Pitch', visible=self.lines_visibility[1])
lroll, = ax.plot(self.shared_data_time[0:self.length_x_vals], self.shared_data_roll[0:self.length_x_vals],
'-ro', lw=1, label='Roll', visible=self.lines_visibility[2])
# storing the lines into a self variable list
self.lines = [lhead, lpitch, lroll]
if self.length_x_vals > 0:
self.set_x_limits()
def get_slider_x_value(self, val):
self.slider_x_value = val
self.set_x_limits()
def set_x_limits(self):
out_min = self.shared_data_time[0] # min output value
out_max = self.shared_data_time[self.length_x_vals - 2] # max output value
x_left_limit = self.slider_x_value * (out_max - out_min) / 90 + out_min # left limit of x axis
x_right_limit = self.shared_data_time[self.length_x_vals - 1] # right limit of x axis
ax.set_xlim(x_left_limit, x_right_limit)
def set_lines_visible(self, label): # it takes label of the button clicked as argument
index = ['Head', 'Pitch', 'Roll'].index(
label) # getting corresponging number of label i.e 0, 1 and 2 for Head, Pitch and Roll
self.lines_visibility[index] = not self.lines_visibility[
index] # if button is clicked then corresponding lines visibility is toggled. It is required while axes is being cleared and updated
self.lines[index].set_visible(not self.lines[index].get_visible()) # toggling lines visibility
plt.draw() # updating the graph when the visibility is toggled
def grid_regulator(self, label):
self.gridregulator = not self.gridregulator
def _start(self, event):
self.supervisor = 'start'
def _stop(self, event):
self.supervisor = 'stop'
def _clear(self, event):
self.supervisor = 'clear'
def save_file(self, event):
if self.supervisor == 'stop':
f = filedialog.asksaveasfile(mode='w', defaultextension='.csv')
if f is None:
return
f.write('Time')
f.write(',')
f.write('Head')
f.write(',')
f.write('Pitch')
f.write(',')
f.write('Roll' + '\n')
for count in range(0, len(self.x_vals)):
try:
f.write(str(self.x_vals[count]))
f.write(',')
f.write(str(self.head[count]))
f.write(',')
f.write(str(self.pitch[count]))
f.write(',')
f.write(str(self.roll[count]) + '\n')
except(IndexError, ValueError):
print("error saving file")
break
f.close()
print("DONE!!!!")
def PlotGraph_process(shared_data_supervisor, shared_data_time, shared_data_head, shared_data_pitch, shared_data_roll):
makegraph = makeGraph(shared_data_supervisor, shared_data_time, shared_data_head, shared_data_pitch,
shared_data_roll)
ani = animation.FuncAnimation(fig, makegraph.animate,
interval=100) # it calls the animate function under the object makegraph after each 1ms interval
# checkbuttons for toggling visibility head, pitch and roll lines
lines_labels = ['Head', 'Pitch', 'Roll'] # label corresponding to each lines
lines_activated = [True, True, True] # initially all the lines visibility is true
lines_checkButton = plt.axes([0.96, 0.90, 0.04, 0.07]) # position of the checkbuttons
lineschxbox = CheckButtons(lines_checkButton, lines_labels, lines_activated)
lineschxbox.on_clicked(makegraph.set_lines_visible)
# checkbutton for turning on and off the minorgrids
minorgrids_labels = ['Minor Grid']
minorgrids_activated = [False]
minorgrids_CheckButton = plt.axes([0.93, 0.97, 0.08, 0.03])
minorgridschxbox = CheckButtons(minorgrids_CheckButton, minorgrids_labels, minorgrids_activated)
minorgridschxbox.on_clicked(makegraph.grid_regulator)
# start button
start = plt.axes([0.8, 0.0, 0.05, 0.03])
startbutton = Button(start, 'START', color='green', hovercolor='gray')
startbutton.on_clicked(makegraph._start)
startbutton.label.set_fontsize(7)
# stop button
stop = plt.axes([0.9, 0.0, 0.05, 0.03])
stopbutton = Button(stop, 'STOP', color='red', hovercolor='gray')
stopbutton.on_clicked(makegraph._stop)
stopbutton.label.set_fontsize(7)
# clear button
clear = plt.axes([0.7, 0.0, 0.05, 0.03])
clearbutton = Button(clear, 'CLEAR', color='yellow', hovercolor='gray')
clearbutton.on_clicked(makegraph._clear)
clearbutton.label.set_fontsize(7)
# save button
save = plt.axes([0.05, 0.0, 0.05, 0.03])
savebutton = Button(save, 'SAVE', color='cyan', hovercolor='gray')
savebutton.on_clicked(makegraph.save_file)
savebutton.label.set_fontsize(7)
# slider for regulating x_lim i.e regulating x axis
sliderX_location = plt.axes([0.15, 0.01, 0.4, 0.02], facecolor='lightgoldenrodyellow')
sliderX = Slider(sliderX_location, 'Slider', 0, 90,
valinit=0) # lowerlimit = 0 and upperlimit = 90 and initialPosition = 0
sliderX.on_changed(makegraph.get_slider_x_value) # calls the get_slider_x_value function if the sliderX is changed
# turn the full screen mode on
# mng = plt.get_current_fig_manager()
# mng.window.state("zoomed") # displaying in zoomed mode
plt.subplots_adjust(left=0.02, right=1, top=1, bottom=0.05) # adjusting the axes dimension
plt.show()