Pikir dan Rasa

cogito ergo sum

Deep Dream » Blog Archive » Matplotlib and PyGTK, live plotting

leave a comment »

I’ve been two days with this to get it working. It’s just a simple Matplotlib plot, using PyGTK to integrate it with a GUI, seems quite simple huh?

Well, it’s my first time using this library, and I’m not good at mathemathics, that’s the excuse. But I’m not the only person asking for this. It’s not clear on the matplotlib documentation how you can do this and there is no examples on the net (surprisingly!)

So, I made this for all the people who needs an example of how to do a matplotlib live plot intergated in GTK using PyGTK.

import matplotlib.pyplot as plt
import gtk
from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
from matplotlib.figure import Figure
import random
import time
import gobject
# We set a simple GTK window (not GTKAgg)
win = gtk.Window()
win.connect("destroy", lambda x: gtk.main_quit())
win.set_title("Prueba de Matplot")
# We create the Figure and the plot
f = Figure(figsize=(5,4), dpi=72)
a = f.add_subplot(111)
# We stablish the values for X (representig time)
x = 0
t = [0]
# We stablish the values for Y (representing any signal)
y = 0
ard1 = [0]
# Create a plot and set some parameters
a.plot(t, ard1 , '-')
# This is the function that waits 1 second to redraw, then sets a random
# value for y and adds it to the 'ard1' array. For 't' we add 1 to the x
# value so it will simulate seconds.
def counter():
    x = 0
    while True:
        y = random.randint(0,1024)
        x += 1
        if x == 60:
            x = 0
        print x,y
        a.plot(t, ard1)
# Create the widget, a FigureCanvas containing our Figure
canvas = FigureCanvas(f)
# This gobject tells GTK to run the function while idle
# And here we go!
I think it’s well commented, but if you have any questions you can mail me at pizted-at-gmail-dot-com
Escrito el 15 de mayo, 2009 en english, matplotlib, pygtk, python.



Written by sunupradana

September 1, 2011 at 1:02 pm

Posted in Komputer

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: