Animated plots in a Tkinter application

Hi,

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
canvas.

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

http://www.scipy.org/Cookbook/Matplotlib/Animations#head-3d51654b8306b1585664e7fe060a60fc76e5aa08

and also examplified in

http://matplotlib.sourceforge.net/examples/animation/animation_blit_tk.html

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
window

I have therefore tried to modify the animation by merging in the
embbedding in Tk example:

http://matplotlib.sourceforge.net/examples/user_interfaces/embedding_in_tk.html

The closest I have gotten to something, which works is this:

import matplotlib
matplotlib.use('TkAgg')

import Tkinter

import sys
import pylab as p
import numpy as npy
import time

root = Tkinter.Tk()
fig = matplotlib.figure.Figure()

ax = fig.add_subplot(111)
canvas = matplotlib.backends.backend_tkagg.FigureCanvasTkAgg(fig, root)
canvas.get_tk_widget().grid()

# create the initial line
x = npy.arange(0,2*npy.pi,0.01)
ax.grid(True)
# canvas.show() #If I add this, it does not show anything?
line, = ax.plot(x, npy.sin(x), animated=True, lw=2)

def run(*args):
    background = canvas.copy_from_bbox(ax.bbox)
    # for profiling
    tstart = time.time()

    while 1:
        # restore the clean slate background
        canvas.restore_region(background)
        # update the data
        line.set_ydata(npy.sin(x+run.cnt/10.0))
        # just draw the animated artist
        ax.draw_artist(line)
        # just redraw the axes rectangle
        canvas.blit(ax.bbox)

        if run.cnt==1000:
            # print the timing info and quit
            print 'FPS:' , 1000/(time.time()-tstart)
            sys.exit()

        run.cnt += 1
run.cnt = 0

manager = p.get_current_fig_manager()
manager.window.after(100, run)

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
it nicely.

Any help would be appreciated.

Kim

First see the embedding_in_tk.py example.
Then, for a simple example, you can see TSPlot here:
http://econpy.googlecode.com/svn/trunk/abm/gridworld/gridworld.py
You can stick it in any frame.
For a more complex example see diagram_cl
http://www.friedrichromstedt.org/index.php?m=186

hth,
Alan Isaac

···

On 5/5/2010 6:53 AM, Kim Hansen wrote:

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?