Animation - Tk


Alright I’ve made some progress here improving upon the example. (Codes at the bottom). So it looks like I’ve started a writing a wrapper class around the plot command that continuously updates it’s data by itself without having to write extra code. To me, using the subplot commands like set_xdata and set_ydata, should automatically update the graph, as I really don’t like dealing with that myself. So what’s happening below is that I’m starting two threads along with the Tkinter user thread. The first thread watches for changes in the data and the second actually induces changes forever. I know that the way I’m doing all this is a) quick and dirty b) really bad c) un-pythonic and d) it closes off all the current methods to plot etc, which if I need to continue developing this, I’ll open them up as well the best I can.

So that brings me to my next question. Is all this necessary? Is there a simpler way to make auto-updating graphs without all of the lines of “outside” code?

One last question, if anyone runs this, you’ll notice that the graph stops updating whenever you use the menu, or move the tkinter window, does anyone have a clue to why this is?

Thank you to everyone for your time.

Kenneth Miller


import sys
import pylab as p
import numpy as npy
import time
import Tkinter as Tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg

import threading

class SimplePlot:
def init(self,root):

   f = matplotlib.figure.Figure(figsize=(5,4),dpi=100) = f.add_subplot(111)
   self.canvas =

   # create the initial line

   canvas = FigureCanvasTkAgg(f, master=root)
   canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

   self.canvas = canvas

   toolbar = NavigationToolbar2TkAgg( canvas, root )
   canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

   manager = p.get_current_fig_manager()

def plot(self,x,y):
line, = p.plot(x,y,animated=True,lw=2)
self.x = x
self.y = y
self.line = line

def run(self,*args):

   class MyThread(threading.Thread):
       def __init__(self,simplePlot):
           self.simplePlot = simplePlot
           print dir(self)
       def run(self):
           background = self.simplePlot.canvas.copy_from_bbox(
       # for profiling
           tstart = time.time()
           self.cnt = 0
           while 1:
               # restore the clean slate background
               # update the data
               # just draw the animated artist
               # just redraw the axes rectangle

               if self.cnt==100:
                   # print the timing info and quit
                   print 'FPS:' , 100/(time.time()-tstart)
                   tstart = time.time()

               self.cnt += 1

   mt = MyThread(self)

class DataThread(threading.Thread):
def run(self):
import time
i = 0.0
while 1:
s.x = x
s.y = npy.sin(x+i/10.0)
i += 1.0

root = Tk.Tk()

s = SimplePlot(root)

#some data points
x = npy.arange(0,2*npy.pi,0.01)
y = npy.sin(x)

dt = DataThread()



end code~~~


On Mar 14, 2008, at 12:46 PM, Kenneth Miller wrote:


I’ve seen the examples for embedding matplot lib in Tk and the Tk animation example. However I’ve not yet been successful with creating an example where i can both animate data and stay in control of Tk. (The animation in tk example seems to lock the mainloop.) Does anyone have any advice or a quick example of a Tk app with an animated graph that doesn’t lock the user out of Tk control?

Kenneth Miller