【发布时间】:2014-02-07 11:33:41
【问题描述】:
对于一个项目,我正在开发一个简单的谐波运动模拟器(质量如何随时间振荡)。我已经正确生成了数据,并且已经在 tkinter 框架中生成了一个图表。目前它只显示一个静态图表,我的目标是随着时间的推移将图表显示为动画。
因此,为了方便起见,我使用以下代码创建了一个程序模型:
#---------Imports
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import tkinter as Tk
from tkinter import ttk
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
#---------End of imports
fig, ax = plt.subplots()
x = np.arange(0, 2*np.pi, 0.01) # x-array
line, = ax.plot(x, np.sin(x))
def animate(i):
line.set_ydata(np.sin(x+i/10.0)) # update the data
return line,
ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), interval=25, blit=False)
#plt.show() #What I want the object in tkinter to appear as
root = Tk.Tk()
label = ttk.Label(root,text="SHM Simulation").grid(column=0, row=0)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.show()
canvas.get_tk_widget().grid(column=0,row=1)
Tk.mainloop()
当 plt.show() 未注释时,此代码将在 tkinter 框架中显示我想要的动画。我希望能够将该动画放置在 tkinter 的框架内。
我也上过 matplotlib 网站,查看了所有动画示例,但没有一个有帮助。我还查看了 Embedding an animated matplotlib in tk 并将 tkinter 按钮放置在 pyplot 图形中,而我想将图形放置在 tkinter 框架中。
所以澄清一下,我希望能够将plt.show() 未注释时产生的动画放在 tkinter 帧中,即root = tk()。
【问题讨论】:
标签: python animation matplotlib tkinter python-embedding