? RE Dynamically updating embedding figures in GTK

I am trying to dynamically update some figures that I

    > have embedded in GTK. The figures display fine and if I
    > minimize and then maximize the window the figures update
    > but I can't get the figures to update dynamically as you
    > watch the window. To try and get this working I used the
    > code from one of the dynamic samples and just embedded it
    > in my application.

Hi Melissa,

You were using kind of a hybrid of what I call the matlab interface
and the application interface, so I rewrote your example in the way I
would do it if I were embedding the dynamic demo in a GTK
application. The code is for the latest release (0.54) where some of
the API changed as discussed in the release notes, and would be
slightly different for earlier versions of matplotlib.

The major difference is that under the new API, you create axes and
subplots from a Figure instance with, for example,

  self.ax = self.figure.add_subplot(111)

Hope this helps,
John

import pygtk
pygtk.require('2.0')
import gtk
import time
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.matlab import *

class C:
    def __init__(self):
        self.figure = Figure( figsize=(8,6), dpi=72)
        self.canvas = FigureCanvas(self.figure) # a gtk.DrawingArea

        self.ax = self.figure.add_subplot(111)

        self.win = gtk.Window()
        self.win.set_name("Embedding in GTK")
        self.win.connect("destroy", gtk.mainquit)
        self.win.set_border_width(5)

        self.win.add(self.canvas)

    def create_AntennaResponseDisplay (self, title):

        ind = arange(1,9)
        p1,p2,p3,p4,p5,p6,p7,p8 = self.ax.bar(ind, self.get_stats())
        centers = ind + 0.5*p1.get_width()
        p1.set_facecolor('b')
        p2.set_facecolor('b')
        p3.set_facecolor('b')
        p4.set_facecolor('b')
        p5.set_facecolor('b')
        p6.set_facecolor('b')
        p7.set_facecolor('b')
        p8.set_facecolor('b')

        self.ax.set_xlim([0.5,9])
        self.ax.set_xticks(centers)
        self.ax.set_ylim([0,100])
        self.ax.set_xticklabels(['1', '2', '3', '4', '5', '6', '7', '8'])
        t = self.figure.text(0.5, 0.95, title,
            horizontalalignment = 'center', fontsize = 10,)

        def updatefig(*args):
            e1, e2, e3, e4, e5, e6, e7, e8 = self.get_stats()

            p1.set_height(e1)
            p2.set_height(e2)
            p3.set_height(e3)
            p4.set_height(e4)
            p5.set_height(e5)
            p6.set_height(e6)
            p7.set_height(e7)
            p8.set_height(e8)

            self.ax.set_ylim([0,100])
            self.canvas.draw()

            return gtk.TRUE

        gtk.timeout_add(250, updatefig)
        self.canvas.show()
        self.win.show()
        gtk.mainloop()
        
    def get_memory(self):
        "Simulate a function that returns system memory"
        return 100*(0.5+0.5*sin(0.5*pi*time.time()))

    def get_cpu(self):
        "Simulate a function that returns cpu usage"
        return 100*(0.5+0.5*sin(0.2*pi*(time.time()-0.25)))

    def get_net(self):
        "Simulate a function that returns network bandwidth"
        return 100*(0.5+0.5*sin(0.7*pi*(time.time()-0.1)))

    def get_stats(self):
        return self.get_memory(), self.get_cpu(), self.get_net(), \
               self.get_memory(), self.get_cpu(), self.get_net(), \
               self.get_memory(), self.get_cpu()

c = C()
c.create_AntennaResponseDisplay('Test!')