How to automatically update a plot?

Hi, folks!

I am using matplotlib (for the first time ever) to embed some graphics in an application written using python + pygtk + glade.
So, I inserted on a page of a notebook a vertical pane, containing a treeview with data on the left pane, and a matplotlib bar graph of this data on the right. I wrote some code to “highlight” the individual rectangle on the graph concerning a given line of the treeview, activated by a click on the treeview’s line (selection of the line). The code is show below, where self.hndBar is a list with the handles to all the Rectangle objects created by the bar method:

def show_component_on_bar_graph( self, widget ):
    selecao = self.treeComp.get_selection()
    ( modelo, iteravel ) = selecao.get_selected()
    if (iteravel ):
        idx = int( self.modeloComposicao.get_value( iteravel, 0 ) )
       
        barraAnterior = self.barraAtual
        if barraAnterior != '':
            print "barraAnterior = ", barraAnterior
            barraAnterior.set_facecolor( 'b' )
            barraAnterior.set_edgecolor( 'b' )
            barraAnterior.draw()
        barraAtual = self.hndBar[ idx - 1 ]
        self.barraAtual = barraAtual
        print "barraAtual = ", barraAtual
        barraAtual.set_facecolor( 'r' )
        barraAtual.set_edgecolor( 'r' )
        barraAtual.draw( )

My problem is: even though I call draw() twice, the figure is not imeediately updated when the user selects a given line on the treeview. The figure is updated only when I change the size of a pane. I tried using matplotlib in interactive mode, adding the line

matplotlib.interactive( True )  

after the lines

import matplotlib  
matplotlib.use( 'GTK' )

but it didn’t work.
How can I make the figure be imeediately updated?

Thanks in advance,

Marcus Vinicius Eiffle Duarte
eiffleduarte@…287…
Niterói, RJ, Brasil

My problem is: even though I call draw() twice, the figure is not
imeediately updated when the user selects a given line on the treeview. The
figure is updated only when I change the size of a pane. I tried using
matplotlib in interactive mode, adding the line

    matplotlib.interactive( True )

You don't need this - -this is for people working interactively from
the python shell, not using mpl in a GUI.

after the lines

    import matplotlib
    matplotlib.use( 'GTK' )

This is fine, but also not necessary. This affects people using
pylab/pyplot. You should not be. You should be following the
"embedding_in_YOURGUI*.py examples at
http://matplotlib.sf.net/examples

barraAtual.draw( )

If I read your post correctly, barraActual is a Rectangle. You need
to be calling draw on the FigureCanvas, not on the individual artists.
Eg

  fig.canvas.draw

where fig is your matplotlib.figure.Figure instance.

JDH

2008/5/20 John Hunter <jdh2358@…2015…87…>:

My problem is: even though I call draw() twice, the figure is not

imeediately updated when the user selects a given line on the treeview. The

figure is updated only when I change the size of a pane. I tried using

matplotlib in interactive mode, adding the line

matplotlib.interactive( True )

You don’t need this - -this is for people working interactively from

the python shell, not using mpl in a GUI.

after the lines

import matplotlib
matplotlib.use( 'GTK' )

This is fine, but also not necessary. This affects people using

pylab/pyplot. You should not be. You should be following the

"embedding_in_YOURGUI*.py examples at

http://matplotlib.sf.net/examples

barraAtual.draw( )

If I read your post correctly, barraActual is a Rectangle. You need

to be calling draw on the FigureCanvas, not on the individual artists.

Eg

fig.canvas.draw

where fig is your matplotlib.figure.Figure instance.

JDH
Thanks, John. That worked fine.

But does this mean that I can’t make the figure repaint just a portion/object, and always will have to repaint/refresh the whole figure (all objects)? Or does matplotlib automatically keep track of what objects were changes and refreshes only these?

Marcus Vinicius Eiffle Duarte
eiffleduarte@…287…
Niterói, RJ, Brasil

If you call fig.canvas.draw, everything will be updated. If you want
to selectively draw certain artists, you can use the animated property
w/ background copy/restore and the draw_artist method as described at

http://www.scipy.org/Cookbook/Matplotlib/Animations

Hope this helps,
JDH

···

On Tue, May 20, 2008 at 8:31 AM, Marcus Vinicius Eiffle Duarte <eiffleduarte@...287...> wrote:

Thanks, John. That worked fine.
But does this mean that I can't make the figure repaint just a
portion/object, and always will have to repaint/refresh the whole figure
(all objects)? Or does matplotlib automatically keep track of what objects
were changes and refreshes only these?