animation.FuncAnimation example --- how does it work?

… keeping conversation on-list …

The reason why you get that error is because you took out the argument from the call signature. But, FuncAnimation assumes that it can send in at least one argument. That argument is not optional, even if you aren’t using it. So, animate() was called with an argument by FuncAnimation internally, but since animate() as defined by you did not have that argument, it fails.

Cheers!
Ben Root

···

On Thu, Apr 23, 2015 at 12:32 PM, Virgil Stokes <vs@…2650…> wrote:

  On 23-Apr-2015 18:25, Benjamin Root

wrote:

            The documentation should say "the number", not "a

number". This particular argument expects either a
generator, an iterable, or an integer. If none is given,
it will try and figure out something for itself. The
integer is used to create a number generator, and so you
effectively have an iterable that supplies the first
argument to the animation function.

Ok Benjamin :slight_smile:
thanks for the clarification.

          Often times, the first argument to this function is just a

frame index. It is quite possible that you don’t need it
at all, but it is a part of the defined API that the
FuncAnimation assumes to be able to call the user-supplied
function.

Does that help?

Yes, this does indeed help. But I am still puzzled by the error

message given when I remove the argument to the animate function.

Ben Root

<shameless_plug>

      P.S. - My new book "Interactive Applications using Matplotlib"

has an entire chapter devoted to animations

</shameless_plug>

      On Thu, Apr 23, 2015 at 12:05 PM,

Virgil Stokes <vs@…2650…>
wrote:

Thanks for your reply to my post, Jerzy.

              On 23-Apr-2015 13:18, Jerzy Karczmarczuk wrote:

Le 23/04/2015 12:22, Virgil Stokes a écrit :

                The following interesting

example (random_data.py) is posted at:

                [http://matplotlib.org/1.4.2/examples/animation/random_data.html](http://matplotlib.org/1.4.2/examples/animation/random_data.html)





                `import matplotlib.pyplot as plt``

                ``import matplotlib.animation as animation``

                `

import numpy as np

` ``

                ``fig, ax = plt.subplots()``

                ``line, = ax.plot(np.random.rand(10))``

                ``ax.set_ylim(0, 1)``

                ``

                ``def update(data):``

                ``    line.set_ydata(data)``

                ``    return line,``

                ``

                ``def data_gen():``

                ``    while True: yield np.random.rand(10)``

                ``

                ``                      ani = animation.FuncAnimation(fig, update,

data_gen, interval=100)``

                ``plt.show()`



                This codes work<small>s; but, </small>                    I am very

confused by it. For example:

                1. There are 3 positional arguments given for `animation.FuncAnimation`                    ;

but, in the

                API documentation for this class ([](http://matplotlib.org/api/animation_api.html)[http://matplotlib.org/api/animation_api.html](http://matplotlib.org/api/animation_api.html)

                ),

only

                two positional arguments are shown.

The third one is the third one,
frames can be a generator, an iterable, or a
number of frames.”
The name “data_gen” could
suggest its meaning (after having read the doc).
Note please that the keyword
parameters are specified extra.

  1. data , the
    argument to the update function seems to
    be undefined.
    FuncAnimation usually passes the frame number: 0, 1,
    2, … as the first parameter of the update function,
    when “frames” is None, or the number of frames. If -
    as here - the third parameter is a generator, it
    passes the yielded data to update.
               It may be used or not.


              Please, in such cases test your programs by adding

some simple tracing contraptions, say, print(data)
inside update.
Yes, I forgot to include this
This makes very little sense to me — what does
“or a number of frames” mean?
I am not sure what you are referencing as “the
doc”; but I did read the documentation several times and
English is my native language.
I am aware of this. Perhaps, I am a Python dummy
— when I see something like value = None in a Python API
argument, I interpret this as a keyword argument and not a
generator.
Ok, I understand that better now. But, you say “or
the number of frames” but the documentation reads
“or a number of frames” — what does this mean?

          And I still do not understand how to use the first

argument of the function to be called for the animation.
In another animation example (histogram.py ), the
animation function is defined by:

          `def animate(i):``

          ``    # simulate new data coming in``

          ``    data = np.random.randn(1000``

          ``    n, bins = np.histogram(data, 100)``

          ``    top = bottom + n``

          ``    verts[1::5,1] = top``

          ``    verts[2::5,1] = top``

          `

          This works of course; but, why is the "`i`              "

required? There is no other reference to it in the entire
script. If I remove it; i.e. use def animate() :
I get the following error:

          `TypeError: animate() takes no arguments (1 given)`



          I do not understand how this explains the fact that the

function no longer has any arguments. Please explain the
meaning of this error message? I did this and more before posting my email. I
would not have posted this unless I thought it was
necessary. And I thought one purpose of this user group
was to help people — even if they ask a question which
may be annoying or “stupid” in some subscribers opinion. I
try to remember what a very wise teacher once said —
“there is no such thing as a stupid question”.


        BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT

        Develop your own process in accordance with the BPMN 2

standard

        Learn Process modeling best practices with Bonita BPM

through live exercises

        [http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual-](http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual-)
        event?utm_

source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF

        _______________________________________________

        Matplotlib-users mailing list

        Matplotlib-users@lists.sourceforge.net

        [https://lists.sourceforge.net/lists/listinfo/matplotlib-users](https://lists.sourceforge.net/lists/listinfo/matplotlib-users)

Ok — thanks again for the clarification. Who knows, with a little
help from this list, I might someday be able to create some useful
animations with Matplotlib Thanks for this also Ben — I will quite likely get this book.
However, considering the complexity of Matplotlib — only 174 pages
bothers me a little.

···

On 23-Apr-2015 18:48, Benjamin Root
wrote:

… keeping conversation on-list …

      The reason why you get that error is because you took out the

argument from the call signature. But, FuncAnimation assumes
that it can send in at least one argument. That argument is
not optional, even if you aren’t using it. So, animate() was
called with an argument by FuncAnimation internally, but since
animate() as defined by you did not have that argument, it
fails.

:wink:

Cheers!

    Ben Root
      On Thu, Apr 23, 2015 at 12:32 PM,

Virgil Stokes <vs@…2650…>
wrote:

On 23-Apr-2015 18:25, Benjamin Root wrote:

                        The documentation should say "the

number", not “a number”. This particular
argument expects either a generator, an
iterable, or an integer. If none is given,
it will try and figure out something for
itself. The integer is used to create a
number generator, and so you effectively
have an iterable that supplies the first
argument to the animation function.

Ok Benjamin :slight_smile:
thanks for the clarification.

                      Often times, the first argument to this

function is just a frame index. It is quite
possible that you don’t need it at all, but it
is a part of the defined API that the
FuncAnimation assumes to be able to call the
user-supplied function.

Does that help?

          Yes, this does indeed help. But I am still puzzled

by the error message given when I remove the argument to
the animate function.

Ben Root

<shameless_plug>

                  P.S. - My new book "Interactive Applications using

Matplotlib" has an entire chapter devoted to
animations

</shameless_plug>

:slight_smile:

                  On Thu, Apr

23, 2015 at 12:05 PM, Virgil Stokes <vs@…2650…>
wrote:

Thanks for your reply to my post, Jerzy.

                          On 23-Apr-2015 13:18, Jerzy Karczmarczuk

wrote:

                          Le 23/04/2015 12:22, Virgil Stokes a

écrit :

                              The following

interesting example (random_data.py )
is posted at:

                              [http://matplotlib.org/1.4.2/examples/animation/random_data.html](http://matplotlib.org/1.4.2/examples/animation/random_data.html)





                              `import matplotlib.pyplot as plt``

                              ``                                    import matplotlib.animation

as animation``

                              `

import numpy as np

` ``

                              ``fig, ax = plt.subplots()``

                              ``                                    line, =

ax.plot(np.random.rand(10))``

                              ``ax.set_ylim(0, 1)``

                              ``

                              ``def update(data):``

                              ``    line.set_ydata(data)``

                              ``    return line,``

                              ``

                              ``def data_gen():``

                              ``                                        while True: yield

np.random.rand(10)``

                              ``

                              ``                                    ani =

animation.FuncAnimation(fig, update,
data_gen, interval=100)``

                              ``plt.show()`



                              This codes work<small>s; but, </small>                                  I

am very confused by it. For example:

                              1. There are 3 positional arguments

given for animation.FuncAnimation ;
but, in the

                              API documentation for this class ([](http://matplotlib.org/api/animation_api.html)


                              ),

only
two positional arguments are shown.
The third one is the third one,
frames can be a generator, an
iterable, or a number of frames.”
The name
“data_gen” could suggest its meaning
(after having read the doc).
Note please that
the keyword parameters are specified
extra.

  1. data ,
    the argument to the update
    function seems to be undefined.
    FuncAnimation usually passes the frame
    number: 0, 1, 2, … as the first
    parameter of the update function, when
    “frames” is None, or the number of
    frames. If - as here - the third
    parameter is a generator, it passes the
    yielded data to update.
                             It may be used or not.


                            Please, in such cases test your programs

by adding some simple tracing
contraptions, say, print(data) inside
update.
Yes, I forgot to include this
This makes very little sense to me
— what does “or a number of frames” mean?
I am not sure what you are
referencing as “the doc”; but I did read the
documentation several times and English is
my native language.
I am aware of this. Perhaps, I am a
Python dummy — when I see something like
value = None in a Python API argument, I
interpret this as a keyword argument and not
a generator.
Ok, I understand that better now.
But, you say “or the number of
frames” but the documentation reads “or ** a
number** of frames” — what does this
mean?

                        And I still do not understand how to use the

first argument of the function to be called
for the animation. In another animation
example (histogram.py ), the
animation function is defined by:

                        `def animate(i):``

                        ``    # simulate new data coming in``

                        ``    data = np.random.randn(1000``

                        ``                                  n, bins = np.histogram(data,

100)``

                        ``    top = bottom + n``

                        ``    verts[1::5,1] = top``

                        ``    verts[2::5,1] = top``

                        `

                        This works of course; but, why is the "`i`                            "

required? There is no other reference to it
in the entire script. If I remove it; i.e.
use def animate() : I get the
following error:

                        `                              TypeError: animate() takes no arguments

(1 given)`

                        I do not understand how this explains the

fact that the function no longer has any
arguments. Please explain the meaning of
this error message? I did this and more before posting
my email. I would not have posted this
unless I thought it was necessary. And I
thought one purpose of this user group was
to help people — even if they ask a
question which may be annoying or “stupid”
in some subscribers opinion. I try to
remember what a very wise teacher once said
— “there is no such thing as a stupid
question”.


                      BPM Camp - Free Virtual Workshop May 6th at

10am PDT/1PM EDT

                      Develop your own process in accordance with

the BPMN 2 standard

                      Learn Process modeling best practices with

Bonita BPM through live exercises

                      [http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual-](http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual-)
                      event?utm_

source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF


                      Matplotlib-users mailing list

                      Matplotlib-users@...1753...forge.net

                      [https://lists.sourceforge.net/lists/listinfo/matplotlib-users](https://lists.sourceforge.net/lists/listinfo/matplotlib-users)

http://matplotlib.org/api/animation_api.html

“Thanks for this also Ben — I will quite likely get this book.
However, considering the complexity of Matplotlib — only 174 pages
bothers me a little. :-)”

I am just that good! No, really though, the book focuses only on a subset of features in Matplotlib, and it does assume that you are somewhat comfortable with the language (or at least, understand the basics of Object Oriented Programming and terminology). There will be another book that will come out in a few months that is intended to be a complete refresh of Sandro Toshi’s 2009 book that might be more up your alley, but I don’t know how much it will cover of Animations. Sandro’s book was released before the animation module was even a twinkle in Ryan May’s eye…

Cheers!

Ben Root

···

On Thu, Apr 23, 2015 at 1:40 PM, Virgil Stokes <vs@…2650…> wrote:

  On 23-Apr-2015 18:48, Benjamin Root

wrote:

… keeping conversation on-list …

      The reason why you get that error is because you took out the

argument from the call signature. But, FuncAnimation assumes
that it can send in at least one argument. That argument is
not optional, even if you aren’t using it. So, animate() was
called with an argument by FuncAnimation internally, but since
animate() as defined by you did not have that argument, it
fails.

Ok --- thanks again for the clarification. Who knows, with a little

help from this list, I might someday be able to create some useful
animations with Matplotlib :wink:

Cheers!

    Ben Root
Thanks for this also Ben --- I will quite likely get this book.

However, considering the complexity of Matplotlib — only 174 pages
bothers me a little. :slight_smile:

      On Thu, Apr 23, 2015 at 12:32 PM,

Virgil Stokes <vs@…2650…>
wrote:

On 23-Apr-2015 18:25, Benjamin Root wrote:

                        The documentation should say "the

number", not “a number”. This particular
argument expects either a generator, an
iterable, or an integer. If none is given,
it will try and figure out something for
itself. The integer is used to create a
number generator, and so you effectively
have an iterable that supplies the first
argument to the animation function.

Ok Benjamin :slight_smile:
thanks for the clarification.

                      Often times, the first argument to this

function is just a frame index. It is quite
possible that you don’t need it at all, but it
is a part of the defined API that the
FuncAnimation assumes to be able to call the
user-supplied function.

Does that help?

          Yes, this does indeed help. But I am still puzzled

by the error message given when I remove the argument to
the animate function.

Ben Root

<shameless_plug>

                  P.S. - My new book "Interactive Applications using

Matplotlib" has an entire chapter devoted to
animations

</shameless_plug>

                  On Thu, Apr

23, 2015 at 12:05 PM, Virgil Stokes <vs@…2650…>
wrote:

Thanks for your reply to my post, Jerzy.

                          On 23-Apr-2015 13:18, Jerzy Karczmarczuk

wrote:

                          Le 23/04/2015 12:22, Virgil Stokes a

écrit :

                              The following

interesting example (random_data.py )
is posted at:

                              [http://matplotlib.org/1.4.2/examples/animation/random_data.html](http://matplotlib.org/1.4.2/examples/animation/random_data.html)





                              `import matplotlib.pyplot as plt``

                              ``                                    import matplotlib.animation

as animation``

                              `

import numpy as np

` ``

                              ``fig, ax = plt.subplots()``

                              ``                                    line, =

ax.plot(np.random.rand(10))``

                              ``ax.set_ylim(0, 1)``

                              ``

                              ``def update(data):``

                              ``    line.set_ydata(data)``

                              ``    return line,``

                              ``

                              ``def data_gen():``

                              ``                                        while True: yield

np.random.rand(10)``

                              ``

                              ``                                    ani =

animation.FuncAnimation(fig, update,
data_gen, interval=100)``

                              ``plt.show()`



                              This codes work<small>s; but, </small>                                  I

am very confused by it. For example:

                              1. There are 3 positional arguments

given for animation.FuncAnimation ;
but, in the

                              API documentation for this class ([](http://matplotlib.org/api/animation_api.html)[http://matplotlib.org/api/animation_api.html](http://matplotlib.org/api/animation_api.html)


                              ),

only

                              two positional arguments are shown.

The third one is the third one,
frames can be a generator, an
iterable, or a number of frames.”
The name
“data_gen” could suggest its meaning
(after having read the doc).
Note please that
the keyword parameters are specified
extra.

  1. data ,
    the argument to the update
    function seems to be undefined.
    FuncAnimation usually passes the frame
    number: 0, 1, 2, … as the first
    parameter of the update function, when
    “frames” is None, or the number of
    frames. If - as here - the third
    parameter is a generator, it passes the
    yielded data to update.
                             It may be used or not.


                            Please, in such cases test your programs

by adding some simple tracing
contraptions, say, print(data) inside
update.
Yes, I forgot to include this
This makes very little sense to me
— what does “or a number of frames” mean?
I am not sure what you are
referencing as “the doc”; but I did read the
documentation several times and English is
my native language.
I am aware of this. Perhaps, I am a
Python dummy — when I see something like
value = None in a Python API argument, I
interpret this as a keyword argument and not
a generator.
Ok, I understand that better now.
But, you say “or the number of
frames” but the documentation reads “or ** a
number** of frames” — what does this
mean?

                        And I still do not understand how to use the

first argument of the function to be called
for the animation. In another animation
example (histogram.py ), the
animation function is defined by:

                        `def animate(i):``

                        ``    # simulate new data coming in``

                        ``    data = np.random.randn(1000``

                        ``                                  n, bins = np.histogram(data,

100)``

                        ``    top = bottom + n``

                        ``    verts[1::5,1] = top``

                        ``    verts[2::5,1] = top``

                        `

                        This works of course; but, why is the "`i`                            "

required? There is no other reference to it
in the entire script. If I remove it; i.e.
use def animate() : I get the
following error:

                        `                              TypeError: animate() takes no arguments

(1 given)`

                        I do not understand how this explains the

fact that the function no longer has any
arguments. Please explain the meaning of
this error message? I did this and more before posting
my email. I would not have posted this
unless I thought it was necessary. And I
thought one purpose of this user group was
to help people — even if they ask a
question which may be annoying or “stupid”
in some subscribers opinion. I try to
remember what a very wise teacher once said
— “there is no such thing as a stupid
question”.


                      BPM Camp - Free Virtual Workshop May 6th at

10am PDT/1PM EDT

                      Develop your own process in accordance with

the BPMN 2 standard

                      Learn Process modeling best practices with

Bonita BPM through live exercises

                      [http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual-](http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual-)
                      event?utm_

source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF


                      Matplotlib-users mailing list

                      Matplotlib-users@lists.sourceforge.net

                      [https://lists.sourceforge.net/lists/listinfo/matplotlib-users](https://lists.sourceforge.net/lists/listinfo/matplotlib-users)