|
From: Virgil S. <vs...@it...> - 2015-04-23 17:47:06
|
On 23-Apr-2015 18:28, Thomas Caswell wrote: > Can everyone please bring the level of snark/hostility down? Programming is > frustrating, but antagonizing the mailing list does not help anyone. > > It is not well documented, but the signature for `func` is assumed to be `def > function(required, *optional_positional)` see > https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/animation.py#L1107 > This was very useful! > for the line where it actually gets called. The logic behind this is that the > function needs to have some input to know what to draw (so the state about > where in the sequence you are lives in the animation code, not in the supplied > user code). > > For passing in the frame data you have several options. The first is to pass > in an iterable of the data you want to be passed to the function (ex > np.arange(5), image_stack, image_generator), basically anything where `it = > iter(input); data = next(it)` 'does the right thing'. The second is to pass > in a callable where repeated calls to `data = my_callable()` 'does the right > thing' This would be useful if you want to reach out and hit some external > hardware for you data each time the animation updates. The last is to pass in > a number of frames, which gets converted into xrange(frames). In all cases, > the Animation turns what ever your input is into an iterable which gets hit > once pre frame (see > https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/animation.py#L1047 > And also very useful! > for the code version of this paragraph). > > Tom > > PS was finishing this up as Ben's email came in > > On Thu, Apr 23, 2015 at 12:06 PM Virgil Stokes <vs...@it... > <mailto:vs...@it...>> 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 >>> >>> >>> import matplotlib.pyplot as plt >>> import matplotlib.animation as animation >> import numpy as np > Yes, I forgot to include this > >>> >>> 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 works; but, 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." > 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. > >> 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. >> >>> 2. 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. > 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? >> >> 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". > ------------------------------------------------------------------------------ > 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- event?utm_ > source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF_______________________________________________ > Matplotlib-users mailing list > Mat...@li... > <mailto:Mat...@li...> > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > |
|
From: Virgil S. <vs...@it...> - 2015-04-24 09:05:12
|
On 23-Apr-2015 20:09, Jerzy Karczmarczuk wrote: > 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. Ok Jerzy --- I apologize for my reaction and you are correct, I should have taken "generator" more seriously. > >>> 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. Yes, again Jerzy. > > 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. Yes, I have written something like this myself in an attempt to better understand Matplotlib animation. IMHO, the documentation for FuncAnimation should explicitly state that one argument is required for the animation function. The documentation does state: "Makes an animation by repeatedly calling a function /func/, passing in (optional) arguments in /fargs/." But of course this is my opinion. > >>> >>> 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. I have also taught programming and I often make the same suggestion and of course this is a suggestion that I follow myself. And I did this before posting. One thing that led me astray was the error message that was generated when I removed the argument to the animate function. I even went into the source of Matplotlib in an attempt to answer this; but, I still could not resolve this issue. > 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. Yes, I did see this and this was one thing that confused me. > > 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. I have done this several times; but, I find this package very complex and difficult to unravel. I use Matplotlib a lot and it is a fantastic package --- great work by the developer team. The fact that I do not always need to know what is "under the cover" of Matplotlib, is IMHO a positive thing, and a good indicator that the documentation (including the examples and gallery) is rather well done. > > 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. I recently obtained this book; but, have not "dived" into it yet It now seems clear that I should take the plunge soon :-) Best regards, and thanks for your follow-up Jerzy, --V |