Animation problem of Virgil Stokes.
Since I began the answer sequence, here's a little more. If this
overlaps other answers, I apologize.
About the third parameter
“frames
can be a generator, an iterable, or a number of frames.”
This makes very little sense to me — what does “or a number of
frames” mean?
The name “data_gen” could suggest its meaning (after having read
the doc).
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 not trying to accuse you of anything, I simply underline that
if the documentation - as cited - mentions the word generator, and
the program contains the argument data_gen, which is OBVIOUSLY a
generator which yields some data, then this might have give you
some clues concerning its meaning.
Note
please that the keyword parameters are specified extra.
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.
1. Generators might be or not function arguments, keyword or not;
this is not an issue here. Generators are entities which produce
iterators. Here you have: ** def data_gen(): while True: yield
np.random.rand(10)** , and this is all.
2. When you see "frames=None", etc., this doesn't absolutely mean
that it is a kw parameter, just a positional parameter with a
default value.
Brendan Barnwell commented this already.
If you look at the definition of the FuncAnimation class, which is
readable, in the file … matplotlib/animation.py, you will see
__ def init(self, fig, func, frames=None, init_func=None,
fargs=None, save_count=None, **kwargs):__ …
which explicitly confirms this.
I think that you got the answers you demanded. The FuncAnimation
callback requires one argument, even if you don’t use it. It may
be the frame number (or index if you wish), or explicit, specific
data, such as in your first example.
If, as in your second example, the parameter passed is the frame
index, and you want to see how you can use it, test this:
**x=np.linspace(0,20,300)**
def update(i):
data=(np.sin(x+i/3)+1)/2
line.set_ydata(data)
** ani = animation.FuncAnimation(fig, update,
interval=40)**
You should see a moving sinusoid.
Please, in such cases test your programs by adding some simple
tracing contraptions, say, print(data) inside update.
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”.
I don't understand your reaction. I teach computer science for many
years, and several times per week I suggest to my students to do
this, to trace their programs. I trace mine. Nobody feels offended.
You said: data is undefined. But it WAS DEFINED, you agree? So,
printing its value was the most natural suggestion. You would
immediately see that it had been generated by data_gen.
Also, I recommend that you look upon the sources of Matplotlib from
time to time. I do it regularly, it is nicely commented, and
complements very well the documentation.
Jerzy Karczmarczuk
PS. Some blizzard around positional/keyword parameters in Python is
quite persistent. “Dive into Python” of Mark Pilgrim covers this,
and offers some useful references.
See also this :
···
https://www.python.org/dev/peps/pep-3102/