I created a GUI to monitor sensor data. The GUI is drawing a canvas over and over again with new data from sensors with xdata as time and ydata as sensor data. When i stop the monitoring, the drawing also stops and shows the last created image. Now when i start the monitoring again, the old data is still shown and overwritten as you can see in the pictures below. I tried various commands as "canvas.get_tk_widget().place_forget()", "canvas.get_tk_widget().destroy()" or "canvas.get_tk_widget().delete(lines1)". But none of them is leading to a new blank canvas, the new plot draws a line from the last datapoint of the previous run even when i delete and recreate the "fig = Figure()" or the masterframe of my canvas. Is there any solution to get rid of the old data/plots and start with a blank canvas again? Below is my code (there is other stuff happening not mentioned which doesnt affect my problem) and two example pictures. I use python 3.8.5 (also tried with python 3.9), matplotlib 3.5.3, tkinter 8.6.10 and windows 10.
The code:
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import tkinter as tk
import numpy as np
dataPlot1 = np.array([])
dataPlot2 = np.array([])
timePlot = np.array([])
def plot_graph():
if condition == True:
root.after(400, plot_graph)
canvas.get_tk_widget().destroy()
if len(ExChange) > 0:
if len(dataPlot1) < number_of_data_points:
timePlot = np.append(timePlot, ExChange[0])
dataPlot1 = np.append(dataPlot1, ExChange[1])
dataPlot2 = np.append(dataPlot2, ExChange[3])
elif len(dataPlot1) == number_of_data_points:
timePlot[0:number_of_data_points - 1] = timePlot[1:number_of_data_points]
timePlot[number_of_data_points - 1] = ExChange[0]
dataPlot1[0:(number_of_data_points - 1)] = dataPlot1[1:number_of_data_points]
dataPlot1[number_of_data_points - 1] = ExChange[1]
dataPlot2[0:(number_of_data_points-1)] = dataPlot2[1:number_of_data_points]
dataPlot2[number_of_data_points-1] = ExChange[3]
if len(timePlot) > 0:
min_value_1 = 0
max_value_1 = 0
min_value_2 = 0
max_value_2 = 0
if PlotCondition[0] == 1:
lines1.set_xdata(timePlot)
lines1.set_ydata(dataPlot1)
if np.amin(dataPlot1) > min_value_1 and min_value_1 == 0:
min_value_1 = np.amin(dataPlot1)
if np.amin(dataPlot1) < min_value_1:
if np.amin(dataPlot1) == 0.0 or np.amin(dataPlot1) == 0:
pass
else:
min_value_1 = np.amin(dataPlot1)
if np.amax(dataPlot1) > max_value_1:
max_value_1 = np.amax(dataPlot1)
else:
lines1.set_ydata([])
lines1.set_xdata([])
if PlotCondition[1] == 1:
lines2.set_xdata(timePlot)
lines2.set_ydata(dataPlot2)
if np.amin(dataPlot2) > min_value_1 and min_value_1 == 0:
min_value_1 = np.amin(dataPlot2)
if np.amin(dataPlot2) < min_value_1:
if np.amin(dataPlot2) == 0.0 or np.amin(dataPlot2) == 0:
pass
else:
min_value_1 = np.amin(dataPlot2)
if np.amax(dataPlot2) > max_value_1:
max_value_1 = np.amax(dataPlot2)
else:
lines2.set_ydata([])
lines2.set_xdata([])
lines1.set_xdata(timePlot)
lines1.set_ydata(dataPlot1)
lines2.set_xdata(timePlot)
lines2.set_ydata(dataPlot2)
canvas.draw()
def get_data():
while condition == True:
ExChange[] = [value1. value2, value3, value4, value5, value6]
def start():
conditiion = True
get_data
plot_graph
def stop():
condition = False
if __name__ == '__main__':
root = tk.Tk()
btn_start = tk.Button(frm_framePanel, image=imageStart, font=('calibri', 12), width=55, height=55,
command=start)
btn_start.place(x=145, y=20)
btn_stop = tk.Button(frm_framePanel, image=imageStart, font=('calibri', 12), width=55, height=55,
command=stop)
btn_stop.place(x=245, y=220)
# ------------------ Create frame for Control ------------------#
frm_frameC = tk.LabelFrame(root, padx=10, pady=10, bg='light grey', bd=3, width=1650, height=800)
frm_frameC.place(x=0, y=0)
# ------------------ Create figure for plotting ------------------#
fig = Figure()
ax = fig.add_subplot(111)
ax.set_title('Data monitoring')
ax.set_xlabel('time in [s]')
ax.set_ylabel('[l/min]')
ax.set_xlim(0, 30)
ax.set_ylim(0, 101)
ax2 = ax.twinx()
ax2.set_ylabel('[l]')
ax2.set_ylim(0, 1001)
lines1 = ax.plot([], [], color='saddlebrown')[0]
lines2 = ax.plot([], [], color='peru')[0]
canvas = FigureCanvasTkAgg(fig, master=frm_frameC)
canvas.get_tk_widget().place(x=5, y=5, width=1000, height=400)
canvas.draw()

