__init_u2;()缺少2个必需的位置参数:“fig”和“func”

2024-09-29 02:16:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我对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()

Tags: posselftimeplotmatplotlibintpyplotball
1条回答
网友
1楼 · 发布于 2024-09-29 02:16:38

在这些情况下,最好的方法是检查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应该是^{}类型的对象。在您的代码中,您还没有声明这样一个对象,所以您必须构造一个

相关问题 更多 >