-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtkinterplot.py
324 lines (259 loc) · 11.3 KB
/
tkinterplot.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import sys
import tkinter as tk
import numpy as np
from numpy import log10, ceil, floor
from colorsys import hsv_to_rgb
from periodics import PeriodicSleeper
class Plot(tk.Frame):
def __init__(self, parent):
self.parent = parent
self.historylen = 100
self.labels = []
self.data = np.array([])
self.scales = np.array([])
self.saved_scales = {} #dict for history of labels and scales, so scale is preserved if data is spotty
self.hues = []
self.w = parent.winfo_width()
self.h = parent.winfo_height()
self.epsilon = 1e-9 #minimum floating value to display
self.max = self.epsilon
self.min = -self.epsilon
self.message = "" #buffer that some external loop updates, then the plotter displays it periodically
self.textoffset = np.array([-10, 0]) #xy offset for the labels
self.temp_tags = [] #strings of y axis mark tags that get redrawn on resize
self.scale_frames = []
self.scale_entries = []
self.paused = False
self.setup_graphics(parent)
self.plotloop()
def setup_graphics(self, parent):
tk.Frame.__init__(self, parent)
parent.wm_title("Serpent Plotter")
parent.wm_geometry("800x400")
self.canvas = tk.Canvas(self, background="gray12")
self.canvas.bind("<Configure>", self.on_resize)
self.yaxis_frame = tk.Frame(parent)
self.yaxis_frame.pack()
self.side_frame = tk.LabelFrame(parent,text='Scale',padx=5, pady=5)
self.side_frame.pack(side=tk.RIGHT, fill='both')
history_frame = tk.Frame(self.side_frame)
history_frame.pack(side=tk.BOTTOM)
historylabel = tk.Label(history_frame, text="# pts:")
historylabel.pack()
historyentry = tk.Entry(history_frame, width=5)
historyentry.insert(0, f"{self.historylen}")
historyentry.pack(side=tk.BOTTOM, before=historylabel)
historyentry.bind("<Return>", lambda event: self.set_history(historyentry))
self.canvas.pack(expand=True, fill=tk.BOTH)
self.pack(expand=True, fill=tk.BOTH)
def pause(self):
self.paused = True
def disp(self, val):
val=(val-self.min)/(self.max-self.min) #map any value to 0 to +1
pad = 20
return (self.h - 2*pad) * (1-val) + pad
def find_nice_range(xmin, xmax):
diff = xmax-xmin
n = ceil(log10(diff / 5) - 1)
s = diff / 10**(n+1)
if s <= 1:
s = 1
elif s <= 2:
s = 2
else:
s = 5
step = s*10**n
bot = floor(xmin/step)*step
try:
return np.arange(bot, xmax+step, step)
except Exception as e:
print(e)
print(xmin, xmax, bot, xmax+step, step)
def on_resize(self, event=None):
self.w = self.winfo_width()
self.h = self.winfo_height()
for temp_tag in self.temp_tags:
self.canvas.delete(temp_tag) # Delete line item from the canvas
for y in Plot.find_nice_range(self.min, self.max):
if abs(y) < self.epsilon:
y = 0
marktag = f"_M{y}"
gridtag = f"_G{y}"
self.temp_tags.append(marktag)
self.temp_tags.append(gridtag)
h = int(self.disp(y))
if y == 0:
self.canvas.create_line(0, self.disp(y), self.w, self.disp(y), tag=gridtag, fill="#AAAAAA")
else:
self.canvas.create_line(0, self.disp(y), self.w, self.disp(y), tag=gridtag, fill="#454545")
self.canvas.create_text(10, h, anchor='w', text=f"{y:0.4g}", tag=marktag)
# self.data = np.zeros_like(self.data)
self.draw()
def str_to_data(message:str):
new_labels = []
new_data = []
lines = message.strip().split("\n")
for line in lines:
try:
parts = line.split(":")
if len(parts) != 2: #ignore lines that don't have exactly one colon
continue
label, value = parts
label = label.strip()
if label in new_labels:
continue #is a duplicate entry in the same message
value = float(value.strip().split(" ")[0]) #only use the first "word" and ignore spaces
new_labels.append(label)
new_data.append(value)
except Exception as e:
print(e)
return new_labels, new_data
def hue_to_hex(hue:float):
rgb = hsv_to_rgb(hue % 1, s=0.5, v=0.8)
hex_code = '#{:02x}{:02x}{:02x}'.format(
int(rgb[0] * 255),
int(rgb[1] * 255),
int(rgb[2] * 255)
)
return hex_code
def set(self, message):
self.message = message
self.paused = False
def set_history(self, entry):
try:
value = int(entry.get())
if value >= 2:
if value < self.historylen:
self.data.resize((len(self.data), value))
else:
#pad the front with zeros
self.data = np.concatenate([np.zeros((len(self.data), value - self.historylen)), self.data], axis=1)
self.historylen = value
except Exception as e:
print(e)
def rescale(self, entry, label):
for scaleframe in self.scale_frames:
label, entry = scaleframe.winfo_children()
label = label['text']
text = entry.get()
try:
value = float(text)
i = self.labels.index(label)
self.scales[i] = value
self.saved_scales[label] = value
print(f"saved scales: {self.saved_scales}")
except Exception as e:
print(e)
def plotloop(self):
'''
- delete any inactive series by removing the label, data, and canvas line
- if there is a new series, add it to the label, data, and canvas line
- recalculate and apply evenly spaced hues to existing lines
'''
new_labels, new_data = Plot.str_to_data(self.message)
if len(new_labels) > 0:
#remove any data and lines that aren't active
to_delete = [] #indexes of labels to delete
for i in range(len(self.labels)):
if self.labels[i] not in new_labels:
self.canvas.delete(f"{self.labels[i]}L") #delete the plotline
self.canvas.delete(f"{self.labels[i]}T") #delete the drawn text
to_delete.append(i)
if to_delete:
for i in sorted(to_delete, reverse=True):
del_label = self.labels.pop(i)
self.scale_frames[i].destroy()
self.scale_frames.pop(i)
print(f"removed series: {del_label}")
self.data = np.delete(self.data, to_delete, axis=0)
self.scales = np.delete(self.scales, to_delete, axis=0)
#add lines that don't exist yet
added_new = False
for new_label in new_labels:
if new_label not in self.labels:
self.labels.append(new_label)
if len(self.data) > 0:
self.data = np.append(self.data, [np.zeros(self.historylen)], axis=0)
self.scales = np.append(self.scales, 1)
else:
self.data = np.array([np.zeros(self.historylen)])
self.scales = np.array([1.])
if new_label in self.saved_scales: #restore the last used scale for this label
self.scales[-1] = self.saved_scales[new_label]
self.canvas.create_line(0,0,0,0, tag=f"{new_label}L", width=2)
self.canvas.create_text(0, 0, anchor="e", tag=f"{new_label}T", text=new_label)
scale_frame = tk.Frame(self.side_frame)
scale_frame.pack()
self.scale_frames.append(scale_frame)
scalelabel = tk.Label(scale_frame, text=new_label)
scalelabel.pack()
scaleentry = tk.Entry(scale_frame, width=5)
scaleentry.insert(0, f"{self.scales[-1]:g}")
scaleentry.pack(side=tk.RIGHT, before=scalelabel)
scaleentry.bind("<Return>", lambda event: self.rescale(scaleentry, scalelabel['text']))
# self.scale_entries.append(scaleentry)
added_new = True
print(f"added new series: {new_label}")
#update line colors if any lines were added
if added_new:
m = len(self.labels)
hues = np.linspace(0, 1, m + 1) #last item is 1, which is the same hue as 0 so it is unused
for i in range(m):
self.canvas.itemconfig(f"{self.labels[i]}L", fill=Plot.hue_to_hex(hues[i]))
self.canvas.itemconfig(f"{self.labels[i]}T", fill=Plot.hue_to_hex(hues[i]))
# print(f"concating {[self.data, np.array([new_data])[:,np.newaxis]]}")
self.data = np.concatenate([self.data[:,1:], np.array(new_data)[:,np.newaxis]], axis=1) #combine shifted old data and new data
self.after_idle(self.draw)
self.after(33, self.plotloop)
def draw(self):
m = len(self.labels)
n = self.historylen
if m == 0 or self.paused:
return
data_scaled = self.data * self.scales.reshape((-1,1))
do_resize = False
max_new = np.max(data_scaled)
if abs(max_new - self.max) > 1e-6:
self.max = max_new
do_resize = True
min_new = np.min(data_scaled)
if abs(min_new - self.min) > 1e-6:
self.min = min_new
do_resize = True
if abs(self.max - self.min) < self.epsilon: #if max-min=0, scale is infinite
self.max += self.epsilon
self.min -= self.epsilon
if do_resize:
self.on_resize()
disp_data = self.disp(data_scaled)
#canvas.coords() takes in a flattened input: x1,y1,x2,y2,...
#next 3 lines adds a column vectors to every other index of the data 2D array which are the x values.
xvals_row = np.linspace(start=0, stop=self.w, num=n)
xvals_arr = np.tile(xvals_row, (len(self.labels), 1))
points = np.dstack([xvals_arr, disp_data]).reshape(m, 2*n)
for i in range(len(self.labels)):
self.canvas.coords(f"{self.labels[i]}L", points[i].tolist())
self.canvas.coords(f"{self.labels[i]}T", (points[i][-2:] + self.textoffset).tolist())
count = 0
def main():
np.set_printoptions(precision=2, suppress=True)
root = tk.Tk()
plot = Plot(root)
import time
def senddata():
global count
count += 1
period = 100
message = ""
for i in range((int)(count / period)+1):
if(i > 4):
break
message = message + f"y{i}: {np.sin(time.time()+i)} \n"
# if count > 5*period:
# count = 0
plot.set(message)
PeriodicSleeper(senddata, 0.01)
root.mainloop()
return 0
if __name__ == '__main__':
sys.exit(main())