<p>我完全被困在这上面了。我的理解是<code>matplotlib.figure</code>用于在GUI中嵌入一个图,然后能够在保持帧不变的情况下更新信息?。这将(在python 2.7上,我认为它是python 3中的<code>tkinter</code>)无错误地运行并生成相同的图。但是我完全不知道为什么他们提供了使用<code>plt</code>更新这个图上的文本的选项,而文本没有显示出来。我明天会再看一遍,但我很高兴有人能在这段时间纠正我,因为文档很混乱。</p>
<pre><code>import matplotlib
import Tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
matplotlib.use("TkAgg")
first_figure = Figure()
plot1_sub1 = first_figure.add_subplot(211)
plot1_sub2 = first_figure.add_subplot(212)
second_figure = Figure()
plot2_sub1 = second_figure.add_subplot(111)
class MainExample(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Example")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
frame = Example(container, self)
self.frames[Example] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(Example)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class Example(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.canvas = FigureCanvasTkAgg(first_figure, self)
self.canvas.get_tk_widget().pack()
self.toolbar = NavigationToolbar2TkAgg(self.canvas, self)
self.toolbar.update()
self.canvas._tkcanvas.pack(fill=tk.BOTH, expand = True)
plot1_sub1.plot([1, 2, 3])
plot1_sub2.plot([4, 5, 6])
plot2_sub1.plot([4, 5, 6])
plt.text(.5,1.5,'211',figure=plot1_sub1)
if __name__ == '__main__':
app = MainExample()
app.geometry("1280x720")
app.mainloop()
</code></pre>