tkinter 中的 pyplot 打开额外的图形,里面没有图
pyplot inside tkinter opens extra figure with no plots inside
大家好,新年快乐。
我的代码需要一些帮助。
我已经将 pyplot 嵌入到 tkinter 中,但每次我调用该函数时,它都会打开并出现新的空图 1、图 2 等。我现在必须关闭图形,但是当我的脚本运行时,每次我需要更新绘图时,它都会打开新的空图形,时间无缘无故地过去了。到目前为止,这是我的代码。在此先感谢您的帮助
def plot_tour(self, tour_tuples):
"""
We call this passing the list of tuples with city
coordinates to plot the tour we want on the GUI
"""
data_in_array = np.array(tour_tuples)
transposed = data_in_array.T
x, y = transposed
self.f, self.a = plt.subplots(1, 1)
self.f = Figure(figsize=(8, 6), dpi=100)
self.a = self.f.add_subplot(111)
self.a.plot(x, y, 'ro')
self.a.plot(x, y, 'b-')
self.a.set_title('Current best tour')
self.a.set_xlabel('X axis coordinates')
self.a.set_ylabel('Y axis coordinates')
self.a.grid(True)
self.canvas = FigureCanvasTkAgg(self.f, master=root)
self.canvas.mpl_connect('motion_notify_event', on_move)
self.canvas.get_tk_widget().grid(row=1, column=1, sticky=W)
plt.close('all')
所以在tcaswell的提议后,多余的数字被删除了。要更新思想,情节需要 canvaw.draw() 和 canvas.show()。完整代码如下
def plot_tour(self, tour_tuples):
"""
We call this passing the list of tuples with city
coordinates to plot the tour we want on the GUI
"""
data_in_array = np.array(tour_tuples)
transposed = data_in_array.T
x, y = transposed
plt.ion()
#self.f, self.a = plt.subplots(1, 1)
self.f = Figure(figsize=(8, 6), dpi=100)
self.a = self.f.add_subplot(111, navigate=True)
self.a.plot(x, y, 'ro')
self.a.plot(x, y, 'b-')
self.a.set_title('Current best tour')
self.a.set_xlabel('X axis coordinates')
self.a.set_ylabel('Y axis coordinates')
self.a.grid(True)
self.canvas = FigureCanvasTkAgg(self.f, master=root)
self.canvas.mpl_connect('motion_notify_event', on_move)
self.canvas.get_tk_widget().grid(row=1, column=1, sticky=W)
self.canvas.draw()
self.canvas.show()
只需删除行
self.f, self.a = plt.subplots(1, 1)
它应该可以正常工作。当您使用 OO 接口将 mpl 嵌入到更大的应用程序中时,您甚至不需要 import pyplot.
这些examples可能也很有用。
大家好,新年快乐。 我的代码需要一些帮助。 我已经将 pyplot 嵌入到 tkinter 中,但每次我调用该函数时,它都会打开并出现新的空图 1、图 2 等。我现在必须关闭图形,但是当我的脚本运行时,每次我需要更新绘图时,它都会打开新的空图形,时间无缘无故地过去了。到目前为止,这是我的代码。在此先感谢您的帮助
def plot_tour(self, tour_tuples):
"""
We call this passing the list of tuples with city
coordinates to plot the tour we want on the GUI
"""
data_in_array = np.array(tour_tuples)
transposed = data_in_array.T
x, y = transposed
self.f, self.a = plt.subplots(1, 1)
self.f = Figure(figsize=(8, 6), dpi=100)
self.a = self.f.add_subplot(111)
self.a.plot(x, y, 'ro')
self.a.plot(x, y, 'b-')
self.a.set_title('Current best tour')
self.a.set_xlabel('X axis coordinates')
self.a.set_ylabel('Y axis coordinates')
self.a.grid(True)
self.canvas = FigureCanvasTkAgg(self.f, master=root)
self.canvas.mpl_connect('motion_notify_event', on_move)
self.canvas.get_tk_widget().grid(row=1, column=1, sticky=W)
plt.close('all')
所以在tcaswell的提议后,多余的数字被删除了。要更新思想,情节需要 canvaw.draw() 和 canvas.show()。完整代码如下
def plot_tour(self, tour_tuples):
"""
We call this passing the list of tuples with city
coordinates to plot the tour we want on the GUI
"""
data_in_array = np.array(tour_tuples)
transposed = data_in_array.T
x, y = transposed
plt.ion()
#self.f, self.a = plt.subplots(1, 1)
self.f = Figure(figsize=(8, 6), dpi=100)
self.a = self.f.add_subplot(111, navigate=True)
self.a.plot(x, y, 'ro')
self.a.plot(x, y, 'b-')
self.a.set_title('Current best tour')
self.a.set_xlabel('X axis coordinates')
self.a.set_ylabel('Y axis coordinates')
self.a.grid(True)
self.canvas = FigureCanvasTkAgg(self.f, master=root)
self.canvas.mpl_connect('motion_notify_event', on_move)
self.canvas.get_tk_widget().grid(row=1, column=1, sticky=W)
self.canvas.draw()
self.canvas.show()
只需删除行
self.f, self.a = plt.subplots(1, 1)
它应该可以正常工作。当您使用 OO 接口将 mpl 嵌入到更大的应用程序中时,您甚至不需要 import pyplot.
这些examples可能也很有用。