last day (15 days later) » 

3:29 PM
0
Q: Funcanimation not working inside figcanvas

WarlockI want to build an interface with certain real-time graphs showing the results of some experiments. For this, I decided to use a combination of glade(UI), gtk, python, and matplotlib. I was working with some basics and I was able to plot some real-time graphs. Now, I have some trouble using Func...

 
@ImportanceOfBeingErnest: I could be wrong, but I think the issue is something else here. Both FuncAnimation and Gtk use event loops. The Gtk.main() starts the Gtk event loop which will control the GUI indefinitely and keep line_ani from getting garbage collected.
 
So did you try to return the funcanimation from the function and put Gtk.main() outside of it?
 
@ImportanceOfBeingErnest: His code does not call plt.show(), so FuncAnimation does nothing.
 
@unutbu Since every GUI uses an event loop, the argument is not clear to me. Why would FuncAnimation work in just any other GUI, but not GTK? What is special about GTK that would prevent FuncAnimation from working?
 
@ImportanceOfBeingErnest: When you call plt.show(), matplotlib's GTK backend eventually calls Gtk.main(). This gives the user control over figure's GUI. But this call to Gtk.main() grabs control of the process, preventing Warlock's other Gtk GUI from ever getting updated. Since Warlock is building his own Gtk GUI (presumably with other widgets besides a matplotlib Figure), he can't call plt.show() .
 
3:29 PM
@unutbu Sorry, but what you are saying is basically that it would not be possible to use FuncAnimation in a GTK GUI. This is clearly wrong. There might be particularities involved which make this harder to use, but there is nothing that should prevent it from being used at all. Or, if there is, this would be a bug, one needs to investigate.
 
Maybe I'm not explaining myself well -- or perhaps I am just wrong (I would love to see a modification of the code I posted that uses FuncAnimation!)
As I see it, calling plt.show with backend set to GTK tells matplotlib to create a mini GTK application with its own GTK window, with a figure embedded.
Warlock is trying to create his own GTK app. He defines his own Window (using Glade) and whatever other widgets he wants. The matplotlib Figure is just one widget inside his app.
If you call plt.show() then matplotlib takes over, creates its own little mini app... and that's it. You don't get the opportunity to build your own full-blown GTK app.
 
3:50 PM
I do not have GTK available nor have I ever worked with it. But I suppose it is not that much different from usual TK. Since it is perfectly possible to create a TK app and put a FigureCanvas in it, which has a FuncAnimation associated, I am indeed assuming that the same must be true for GTK as well. So if the code in the question fails, it would be due to the specifics of this code, not some general eventloop thing.
My first idea was of course that the reference to the FuncAnimation gets garbage collected. Did you actually test if this was the case?
If this is not the source of the problem, my next best guess is that you need to instantiate FigureCanvas before creating the FuncAnimation. Also not using plt at all would be a good idea (while this shouldn't break anything, it still makes the code cleaner to use matplotlib.figure.Figure instead of plt.figure())
 
Ugg, I think I've got egg on my face. Yes, your idea works: ideone.com/wP2Um1
could you repeat the link to the dupe target?
nvm
Thanks for your help; I've learned something today
 
4:07 PM
Well, you made your version work, which uses all of my ideas. Does this tell us that the duplicate would indeed be justified? Or is it rather one of the other two suggestions? The dupe is still linked under "linked questions".
 
I've got to go now; but will think about this some more.
 
hai
 
4:48 PM
@ImportanceOfBeingErnest: All three of your suggestions seem correct to me. In my toy example, FigureCanvas must be created before creating FuncAnimation. And using matplotlib.figure.Figure is required; plt.figure leads to a Gtk-Error.
 
In the OP there is also plt.figure being used, not leading to an error. (The reason could be that the questioner uses GTK as its standard backend and you are not?). In any case if this is really not just about keeping the reference, this question might deserve its own answer, mentionning all three parts that led to the solution.
 

last day (15 days later) »