__init__() 缺少 2 个必需的位置参数:'fig' 和 'func'

__init__() missing 2 required positional arguments: 'fig' and 'func'

我是 python 的新手,刚开始使用 类。 我正在 Python 中构建一个移动的球,但出现此错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-1-7cff16ca52e8> in <module>
     33     matplotlib.pyplot.plot(pos,5,'bo', markersize=5)
     34 
---> 35 animator=matplotlib.animation.FuncAnimation()

TypeError: __init__() missing 2 required positional arguments: 'fig' and 'func'

这是我的代码:

%pylab inline

time = 0


class Ball:
    def __init__(self,xvelocity=1, yvelocity=1, inx=1,iny=5, posx=[],posy=[]):
        self.xvelocity=xvelocity
        self.yvelocity=yvelocity
        #giving velocity
        self.inx=inx
        self.iny=iny
        #defining starting position
        self.posx = [self.inx]
        self.posy = [self.iny]
        

ball=Ball()
x=[0, 10]
y=[0, 10]
pos=int(ball.xvelocity)*int(time)
matplotlib.pyplot.plot(x,y, 'ro', markersize=0.0001)
for time in range(15):

   
    posx=int(ball.xvelocity)*int(time)
    if pos!=10:
        pos=pos%10

    matplotlib.pyplot.plot(pos,5,'bo', markersize=5)
    
animator=matplotlib.animation.FuncAnimation()

在这些情况下,最好的办法是检查 documentation。我们在函数签名中看到:

class matplotlib.animation.FuncAnimation(fig, func, frames=None, init_func=None, fargs=None, save_count=None, *, cache_frame_data=True, **kwargs)[source]

这表明它需要两个必需的参数,按顺序 figfunc,然后是可选参数列表。在您的代码中,您没有传递任何参数:

animator=matplotlib.animation.FuncAnimation()     

因此需要更新以包含那些有效的参数。上面的文档应该可以让您很好地了解哪些对象是有效的。例如,我们看到 fig 应该是 Figure 类型的对象。在你的代码中你还没有声明这样一个对象,所以你必须构造一个。