2017-10-17 60 views
-1

我是python的新手,所以我明白这可能是一个愚蠢的问题,但我遇到了动画问题。我看不出错误是什么。我得到这个错误 TypeError:f()缺少1所需的位置参数:'Python,使用两个参数动画一个函数

我想在动画时使用matplotlib,因为我没有下载scitools。

任何帮助都将是非常大大appriciated

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
from matplotlib import style 

x = np.linspace(-6, 6) 
tmax = 1 
tmin = -1 
t = np.linspace(-1, 1) 

def f(x, t): 
    term = (np.exp(-1*(x-3*t)**2))*np.sin(3*np.pi*(x-t)) 
    return term 
max_f = f(x[-1], t[-1]) 
plt.ion() 
y = f(x, tmax) 
lines = plt.plot(x, y) 
plt.axis([x[0], x[-1], -0.1, max_f]) 
plt.xlabel('x') 
plt.ylabel('f') 

counter = 0 
for ts in t: 
    y = f(x, t) 
    lines[0].set_ydata(y) 
    plt.legend(['ts=%4.2f' % ts]) 
    plt.draw() 
    plt.savefig('tmp_%04d.png' % counter) 
    counter += 1 


fig = plt.figure() 

anim = animation.FuncAnimation(fig, f, interval = 1000, blit=True) 
fig = plt.figure() 
plt.axis([x[0], x[-1], -0.1, max_f]) 
lines = plt.plot([], []) 
plt.xlabel('x') 
plt.ylabel('f') 

plt.show() 

编辑,完全回溯: 异常在Tkinter的回调 回溯(最近通话最后一个): 文件“C:\用户\我\ AppData的。\本地\程序\ Python的\ Python36-32 \ LIB \ tkinter__init __ PY “线1699,在通话 回报self.func(*参数)

文件” C:\用户\我\应用程序数据\本地\ Programs \ Python \ Python36-32 \ lib \ tkinter__init __。py“,行745,在CALLIT FUNC(*参数)

文件 “C:\用户\我\应用程序数据\本地\程序\ Python的\ Python36-32 \ LIB \站点包\ matplotlib \后端\ backend_tkagg.py”第370行,在idle_draw中 self.draw()

文件“C:\ Users \ me \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ backends \ backend_tkagg.py” ,第351行,绘制 FigureCanvasAgg.draw(self)

文件“C:\ Users \ me \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ backends \ backend_agg。 py“,行464,绘制 self.figure.draw(self.rende rer)

draw_wrapper draw(artist,rer)文件“C:\ Users \ me \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ artist.py” renderer,* args,** kwargs)

文件“C:\ Users \ me \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ figure.py”,第1151行, in draw self.canvas.draw_event(renderer)

文件“C:\ Users \ me \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ backend_bases.py”,行1823,draw_event self.callbacks.process(s,event)

代理(* args,**)文件“C:\ Users \ me \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ cbook.py”,第554行kwargs)

文件 “C:\用户\我\应用程序数据\本地\程序\ Python的\ Python36-32 \ LIB \站点包\ matplotlib \ cbook.py”,线路416,在通话 回报MTD (* args,** kwargs)

文件“C:\ Users \ me \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ animation.py”,行881,in _start self._init_draw()

文件“C:\ Users \ me \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ animation.py”,行1540,_init_draw self._draw_frame(next(self。new_frame_seq()))

文件“C:\ Users \ me \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ matplotlib \ animation.py”,第1562行,在_draw_frame中 self ._drawn_artists = self._func(framedata,* self._args)

类型错误:F()失踪1个人需要的位置参数: 'T'

+0

你可能想找出传给你的错误就行了。 – pvg

+0

请发布完整的追溯,而不仅仅是错误。 – roganjosh

+0

另外,编辑问题。 –

回答

0

至于说,这是不是真正的错误,你可以很容易地通过在FuncAnimation中为fargs提供t的一些值来防止这种情况。但是,这不会导致代码生成动画,因此如前所述,从exmaple开始逐步添加您的函数和代码并查看会发生什么。

这将最终导致类似以下内容:

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

x = np.linspace(-6, 6) 
tmax = 1 
tmin = -1 
t = np.linspace(-1, 1) 

def f(x, t): 
    term = (np.exp(-1*(x-3*t)**2))*np.sin(3*np.pi*(x-t)) 
    return term 

y = f(x, tmax) 
lines = plt.plot(x, y) 
plt.axis([x[0], x[-1], -1, 1]) 
plt.xlabel('x') 
plt.ylabel('f') 

counter = [0] 
def animate(ts): 
    y = f(x, ts) 
    lines[0].set_ydata(y) 
    plt.legend(['ts=%4.2f' % ts]) 
    #plt.savefig('tmp_%04d.png' % counter) 
    counter[0] += 1 

anim = animation.FuncAnimation(plt.gcf(), animate, frames = t, interval = 1000) 
plt.show() 

enter image description here

相关问题