【问题标题】:Python, animate a function with two argumentsPython,用两个参数为函数设置动画
【发布时间】:2017-10-17 15:16:39
【问题描述】:

我是 python 新手,所以我知道这可能是一个愚蠢的问题,但我在动画这个问题上遇到了问题。我看不出错误是什么。我收到这个错误 类型错误:f() 缺少 1 个必需的位置参数:'

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

任何帮助都会非常感谢

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:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\tkinter__init__.py”,第 1699 行 返回 self.func(*args)

文件“C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\tkinter__init__.py”,第 745 行,在 callit 函数(*args)

文件“C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\backends\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.renderer)

文件“C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\artist.py”,第 63 行,在 draw_wrapper 绘制(艺术家,渲染器,*args,**kwargs)

文件“C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\figure.py”,第 1151 行,绘制中 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)

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

调用中的文件“C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\cbook.py”,第 416 行 返回 mtd(*args, **kwargs)

文件“C:\Users\me\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\animation.py”,第 881 行,在 _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)

TypeError: f() 缺少 1 个必需的位置参数:'t'

【问题讨论】:

  • 您可能想找出导致错误的行。
  • 请发布完整的回溯,而不仅仅是错误。
  • 另外,编辑问题。
  • 代码和平常的FuncAnimation examples完全不同。即使它没有错误地运行,它也不会产生动画。也许你想退后一步,确保代码的设计有意义。

标签: python function animation matplotlib


【解决方案1】:

如前所述,这与错误无关,您可以通过在 FuncAnimation 中为 t 提供一些值作为 fargs 来轻松防止错误。但是,这根本不会导致代码产生动画,因此如前所述,从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()

【讨论】:

    猜你喜欢
    • 2012-03-30
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 2017-05-06
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    相关资源
    最近更新 更多