非常感谢您的帮助。
我正在尝试在移动滑块时更新matplotlib imshow图(Python 3.7.4),但是调用update函数时并没有任何变化,尽管调用了canvas.draw,我认为这是我需要解决的全部事情问题。该代码也需要嵌入到Tkinter中。此代码将重现该问题:
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
class mainApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.image = np.zeros((10, 10))
fig = Figure()
self.mainAx = fig.add_subplot(111)
self.drawing = self.mainAx.imshow(self.image)
self.graphCanvas = FigureCanvasTkAgg(fig, master=self)
self.graphCanvas.draw()
self.graphCanvas.get_tk_widget().pack(side="top", fill="both", expand=True)
self.slider = tk.Scale(master=self, command=self.updateGraph, orient="horizontal")
self.slider.pack(fill="x")
def updateGraph(self, e):
self.image = np.zeros((10, 10))
self.image[self.slider.get()//10, self.slider.get()%10] = 1
self.drawing.set_data(self.image)
self.graphCanvas.draw_idle()
main = mainApp()
main.mainloop()
调用mainAx.imshow(self.image)可以,但是这要慢得多,我希望该程序尽可能快。我认为问题出在draw_idle上,但我不知道该怎么办。它也不适用于常规的canvas.draw()函数
谢谢,亚当
将您的更新功能更改为此。
def updateGraph(self, e):
self.image = np.zeros((10, 10))
self.image[self.slider.get()//10, self.slider.get()%10] = 1
self.drawing = self.mainAx.imshow(self.image)
self.graphCanvas.draw()
我还将修改您的命令以等待直到在滑块上选择了最终值,除非您希望看到它更改,否则您可以在这里找到如何通过事件绑定或延迟来执行此操作。 TkInter, slider: how to trigger the event only when the iteraction is complete?