0
大家好,祝你有個美好的新年。 我需要一些幫助,我的代碼。 我已經在tkinter中嵌入了pyplot,但每次調用該函數時,都會打開並顯示新的空白figure1,figure2等等。我現在必須關閉這些數字,但是當我的劇本運行時,每次我需要更新劇情時,它都會打開新的空白數字,並且無需理會時間。這是我的代碼到目前爲止。在此先感謝您的幫助tkinter裏面的pyplot打開額外的數字,裏面沒有地塊
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()
感謝您的建議,我這樣做,但現在情節不更新。但我添加了canvas.draw()和canvas.show(),現在像一個魅力。謝謝。 – SotirisTsartsaris
當你通過用戶交互改變軸(改變限制,addidng藝術家等)時,你將需要手動觸發'draw'。增加了額外的鏈接。 – tacaswell