Could anyone give a working example of an embedded, animated plot in a
Tkinter application, where animated=True is used together with canvas
background copying to make efficient animated plots in Tkinter
together with other widgets?
I cannot make it work myself, see below.
I am working on a prototype, where have some Tkinter widgets to
control what is plotted. What I want to plot is something which
progress in time depending on the state of the widgets, and it is
potentially a lot of graphs and details on the canvas, but only minor
changes between updates. So I would rather avoid redrawing the whole
I would therefore like to use the trick with copying the backgorund,
static canvas from frame to frame to a buffer, and only draw the
animated artists on top of it as is discussed in
and also examplified in
Now, the example does not embed the matplotlib canvas in a Tkinter
root class, which I need if I want to add other Tk widgets on the same
I have therefore tried to modify the animation by merging in the
embbedding in Tk example:
The closest I have gotten to something, which works is this:
import pylab as p
import numpy as npy
root = Tkinter.Tk()
fig = matplotlib.figure.Figure()
ax = fig.add_subplot(111)
canvas = matplotlib.backends.backend_tkagg.FigureCanvasTkAgg(fig, root)
# create the initial line
x = npy.arange(0,2*npy.pi,0.01)
# canvas.show() #If I add this, it does not show anything?
line, = ax.plot(x, npy.sin(x), animated=True, lw=2)
background = canvas.copy_from_bbox(ax.bbox)
# for profiling
tstart = time.time()
# restore the clean slate background
# update the data
# just draw the animated artist
# just redraw the axes rectangle
# print the timing info and quit
print 'FPS:' , 1000/(time.time()-tstart)
run.cnt += 1
run.cnt = 0
manager = p.get_current_fig_manager()
p.show() # If I outcomment this, the graph does not animate
Now, this does show an efficient animated plot embedded in a Tk
application, but I cannot make it work witout instatiating the other
annoying backgroud window, whcih pops up when the p.show() is done.
However, if i uncomment it, I never get a visible window.
As I have understood the cookbook, one should draw the canvas before
copying it to the background and before drawing the animated Artists.
However, if I do that it does not work either.
I must admit, that I do not fully grasp waht is goin on in the lines,
where the current figure manager is associated woth the run method.
Another problem with the naimation is that it is not possible to close
Any help would be appreciated.