2014-01-17 3 views
3

프로젝트의 경우 단순한 고조파 모션 시뮬레이터 (질량이 시간에 따라 어떻게 진동하는지)를 연구 중입니다. 데이터를 올바르게 생성했으며 tkinter 프레임 작업 내에서 그래프를 생성했습니다. 현재로서는 일정 기간 동안 그래프를 애니메이션으로 표시하는 정적 인 그래프 만 보여줍니다.matplotlib 애니메이션을 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() 

나는 Tkinter의 프레임 워크에서 원하는 애니메이션을 화면에 표시하는 코드 때 plt.show() 주석 처리되지 않았습니다. 그 애니메이션을 tkinter의 틀 안에 넣을 수 있기를 바랍니다.

나는 또한 matplotlib 웹 사이트에 있었고 모든 애니메이션 예제를 보았지만 아무도 도움을받지 못했습니다. 나는 또한이 웹 사이트에서 '21179971'이라는 질문을 살펴본 결과 파이 그래프 (pyplot) 그림 안에 tkinter 버튼을 놓았지만, 그림은 tkinter 프레임 안에 넣고 싶습니다. "#의 plt.show는()", 즉 Tkinter를 프레임에 주석 때

그래서 단지 명확히하기 위해, 내가 제작 한 애니메이션을 배치 할 수 있도록하고 싶습니다 (루트 = TK는())

답변

4

나는 수정 내 코드 :

#---------Imports 
from numpy import arange, sin, pi 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 
from matplotlib.figure import Figure 
import Tkinter as Tk 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
#---------End of imports 

fig = plt.Figure() 

x = np.arange(0, 2*np.pi, 0.01)  # x-array 

def animate(i): 
    line.set_ydata(np.sin(x+i/10.0)) # update the data 
    return line, 

root = Tk.Tk() 

label = Tk.Label(root,text="SHM Simulation").grid(column=0, row=0) 

canvas = FigureCanvasTkAgg(fig, master=root) 
canvas.get_tk_widget().grid(column=0,row=1) 

ax = fig.add_subplot(111) 
line, = ax.plot(x, np.sin(x)) 
ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), interval=25, blit=False) 

Tk.mainloop() 
+0

감사합니다. 내가 읽은 모든 것보다 확실히 도움이된다. :) – user3208454

관련 문제