Bonjour, je suis entrain de faire un code me permettant d’afficher mes données en direct.... Jusqu'à là tout va bien, cela marche parfaitement avec matplotlib mais le problème c'est que dès lors que je souhaite l'afficher sur canvas,
cela ne marche pas et tkinter plante littéralement....
voici mon code qui marche correctement sous matplotlib:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import serial import numpy as np import matplotlib.pyplot as plt from drawnow import * import tkinter as tk from matplotlib.ticker import MultipleLocator, FormatStrFormatter app = tk.Tk() def launch_Graph(): tempC = [] arduino_Data = serial.Serial("com4",9600) plt.ion() #interactive mode (live data) cnt=0 def makeFig(): ay = plt.axes() ay.yaxis.set_major_locator(MultipleLocator(1)) ay.yaxis.set_minor_locator(MultipleLocator(0.5)) plt.ylim(0,30) plt.title("Temperature as function time") plt.grid(True) plt.ylabel("Temp (°C)") plt.xlabel("Time (s)") plt.plot(Data) """ plt.plot(Data,"rx-",label="Degrees") plt.legend(loc="upper left") """ while True: tempC.append(str(arduino_Data.readline())) def cleanData(L): newL = [] for i in range(len(L)): temp=L[i][2:] newL.append(float(temp[:-5])) return newL Data = cleanData(tempC) drawnow(makeFig) # for draw the figure plt.pause(.000001) cnt+=1 if(cnt>50): # if there is 50 time the same value matplotlib set pause Data.pop(0) btn = tk.Button(app,command=launch_Graph).pack() app.mainloop()
et celui qui plante.... :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import serial import numpy as np import matplotlib.pyplot as plt from drawnow import * import tkinter as tk from matplotlib.ticker import MultipleLocator, FormatStrFormatter from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk app = tk.Tk() def launch_Graph(): tempC = [] arduino_Data = serial.Serial("com4",9600) f = Figure(figsize=(6,4), dpi=100) a_1 = f.add_subplot(111) def makeFig(): line_graphic_1, =a_1.plot(Data) while True: tempC.append(str(arduino_Data.readline())) def cleanData(L): newL = [] for i in range(len(L)): temp=L[i][2:] newL.append(float(temp[:-5])) return newL Data = cleanData(tempC) canvas = FigureCanvasTkAgg(f, app) canvas.draw() canvas.get_tk_widget().place(x=500, y =100 ,width=800, height=800) toolbar = NavigationToolbar2Tk(canvas, app) toolbar.update() canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1) btn = tk.Button(app,command=launch_Graph).pack() app.mainloop()
Merci d'avance,
bonne journée
Partager