我试图在一个循环中删除嵌入在tkinter中的matplotlib图,并每2秒被另一个matplotlib图所取代。但是不是删除每个图形,而是将图形放在彼此的顶部,并且只在循环结束时删除它们。
root = tk.Tk()
root.geometry("600x500")
fig, ax = plt.subplots(nrows = 3, ncols = 1, figsize = (6, 4))
t = np.linspace(0,10,len(X_EEG1[0]))
for data1, data2, data3, datay in zip(X_EEG1_ten, X_EEG2_ten, X_EMG_ten, y_ten):
ax[0].plot(t, data1, color = 'w', lw = 0.5)
ax[1].plot(t, data2, color = 'w', lw = 0.5)
ax[2].plot(t, data3, color = 'grey', lw = 0.5)
plt.tight_layout()
canvas = FigureCanvasTkAgg(fig, master = root)
canvas.draw()
canvas.get_tk_widget().pack()
root.update() # updating window to show figure
time.sleep(2) # wait 2 seconds
canvas.get_tk_widget().destroy() # destroy figure
root.update() # updating window to show destroyed figure
root.mainloop()
任何帮助我都很感激,谢谢。
我明白了!👍🏻
root = tk.Tk()
root.geometry("600x500")
plt.style.use('dark_background')
fig, ax = plt.subplots(nrows = 3, ncols = 1, figsize = (6, 4))
t = np.linspace(0,10,len(X_EEG1[0]))
ax0, = ax[0].plot(t, X_EEG1_ten[0], color = 'w', lw = 0.5)
ax1, = ax[1].plot(t, X_EEG2_ten[0], color = 'w', lw = 0.5)
ax2, = ax[2].plot(t, X_EMG_ten[0], color = 'grey', lw = 0.5)
for data1, data2, data3, datay in zip(X_EEG1_ten, X_EEG2_ten, X_EMG_ten, y_ten):
ax0.set_xdata(t)
ax0.set_ydata(data1)
ax1.set_xdata(t)
ax1.set_ydata(data2)
ax2.set_xdata(t)
ax2.set_ydata(data3)
plt.tight_layout()
canvas = FigureCanvasTkAgg(fig, master = root)
canvas.get_tk_widget().grid(row=0,column=0)
canvas.draw()
root.update()
canvas.flush_events() # critical missing piece here!
time.sleep(2)
root.mainloop()