redraw during events?

Hi,

I would like to react on mouse-clicks
by adding lines to a plot
(see the example at the end of this mail).

However, the additional lines, plotted inside
the on_press event handler, are not shown
(only after clicking on the "home" button, or zoom, they
appear). This is with matplotlib 0.80, debian, GTKAgg frontend.

Is there a simple trick to make them visible?

Many thanks in advance,

Arnd

# ---------------------------------
from pylab import *

phi=arange(0.0,2.0*pi+0.05,0.1)
x=cos(phi)
y=sin(phi)

w,h=12,6
fig=figure(figsize=(w, h), dpi=100)
ax1 = fig.add_axes([0.1, 0.55, 0.8, 0.4])
plot(phi,x)

ax2 = fig.add_axes([0.1, 0.1, 0.35*h/w, 0.35])
plot(x,y,linewidth=5)
title ("circular circle?")
xlim(-1.1,1.1 )
ylim(-1.1,1.1 )
xlabel("click in this plot window")

def on_press(event):
    """Draw on button press events."""

    print "Event:",event.x,event.y,event.xdata,event.ydata,event.inaxes
    if event.inaxes==ax2:
        ax1.plot(phi, event.ydata*x, 'r--')
        # QUESTION: How can I force to show this line in ax1?

connect('button_press_event', on_press)

show()

You either need to turn on interactive plotting (`ion()', I think), or add a call to `draw()' to redraw the plot:

def on_press(event):
     """Draw on button press events."""

     print "Event:",event.x,event.y,event.xdata,event.ydata,event.inaxes
     if event.inaxes==ax2:
         ax1.plot(phi, event.ydata*x, 'r--')
         draw()

I hope this helps.

Ken

···

On Jun 6, 2005, at 9:10 AM, Arnd Baecker wrote:

However, the additional lines, plotted inside
the on_press event handler, are not shown
(only after clicking on the "home" button, or zoom, they
appear). This is with matplotlib 0.80, debian, GTKAgg frontend.

Quoting Arnd Baecker <arnd.baecker@...273...>:

I would like to react on mouse-clicks
by adding lines to a plot
(see the example at the end of this mail).

However, the additional lines, plotted inside
the on_press event handler, are not shown
(only after clicking on the "home" button, or zoom, they
appear). This is with matplotlib 0.80, debian, GTKAgg frontend.

Is there a simple trick to make them visible?

I think so:

    if event.inaxes==ax2:
        ax1.plot(phi, event.ydata*x, 'r--')
        # QUESTION: How can I force to show this line in ax1?

          # ANSWER
          draw()

connect('button_press_event', on_press)

At least on my simple tests, that does it.

Best,

f

> However, the additional lines, plotted inside
> the on_press event handler, are not shown
> (only after clicking on the "home" button, or zoom, they
> appear). This is with matplotlib 0.80, debian, GTKAgg frontend.

You either need to turn on interactive plotting (`ion()', I think), or

This one does not work for me.

add a call to `draw()' to redraw the plot:

Yes - this does the job.

def on_press(event):
     """Draw on button press events."""

     print "Event:",event.x,event.y,event.xdata,event.ydata,event.inaxes
     if event.inaxes==ax2:
         ax1.plot(phi, event.ydata*x, 'r--')
         draw()

I hope this helps.

It does indeed.
Now I am only wondering, why draw() did not work an hour ago or
so, when I played around with the various options?
(Obviously, this is a less technical, but more difficult question ;-).

Many thanks, also to you Fernando,

Arnd

···

On Mon, 6 Jun 2005, Ken McIvor wrote:

On Jun 6, 2005, at 9:10 AM, Arnd Baecker wrote: