Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a Python function without passing it an argument - animate(i)

I'm struggling to work out what's going on in this code and adapt it to get my own code to work. I'm not sure if it's something universal to Python functions or if it's something specific to the matplotlib animation package.

The code comes from Jake Vanderplas animation tutorial but similar examples of it around the web.

This is the animation function which is called:

# animation function.  This is called sequentially
def animate(i):
    x = np.linspace(0, 2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    return line,

and this is the line that calls it:

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=20, blit=True)

animate takes the argument i but he is calling it without the variable i. Does animation.FuncAnimation somehow know to pass a value in there or how does this work?

Small aside - is that comma after the return statement of any significance?

like image 685
cardamom Avatar asked Feb 28 '26 21:02

cardamom


2 Answers

When seeing cases like this one, it's good to remember that functions, like everything else in Python, are objects. As such, they can be, among other things, stored in data structures, be re-assigned a name and be passed as another functions argument.

Take a look at the documentation for animation.FuncAnimation:

class matplotlib.animation.FuncAnimation(fig, func, frames=None, init_func=None, fargs=None, save_count=None, **kwargs) Bases: matplotlib.animation.TimedAnimation

Makes an animation by repeatedly calling a function func, passing in (optional) arguments in fargs.

So it simply takes the function object animate (in this case) and calls it with the arguments supplied in fargs (which if not specified, default to None).

Note: After viewing the source for FuncAnimation I realized that fargs are simply additional arguments to be passed to the function animate and if fargs=None they default to an empty tuple meaning no arguments. (In this case fargs should not indeed be specified since animate takes only one argument)

The function animate is called using: func(framedata, *self._args) where framedata relates to the frames argument passed initially and self._args are any arguments supplied via fargs.

The full call made (self._func in the following is actually your animate function):

 # Call the func with framedata and args. If blitting is desired,
 # func needs to return a sequence of any artists that were modified.
 self._drawn_artists = self._func(framedata, *self._args)  

So yup, internally matplotlib calls your function with the framedata argument.


As the trailing comma in the return, that means that it will return the value as a tuple with a single element, line in this case. It's the same as the following:

>>> i = 1,
>>> i
(1,)  # tuple with single element
like image 71
Dimitris Fasarakis Hilliard Avatar answered Mar 02 '26 11:03

Dimitris Fasarakis Hilliard


is that comma after the return statement of any significance?

yes, it is shorthand syntax for tuple and so...
return line, == return (line,)

Does animation.FuncAnimation somehow know to pass a value in there or how does this work?

I have not looked into matplotlib, but I think you are confused about the syntax of it. One can pass in function as an argument. See below

def foo(func,x,y,z): # foo takes in func,x,y,z
    return func(z) # foo returns func(z)
    
def bar(a): # bar takes in a, return a*a
    return a*a
    
print foo(bar,2,3,4) # foo returns bar(4)
like image 42
taesu Avatar answered Mar 02 '26 11:03

taesu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!