LineBuilder

All,

I am curious why this doesn't work:

# linebuilder.py

import matplotlib.pyplot as plt

class LineBuilder:
    def __init__(self, line):
        self.line=line
        self.xs=list(line.get_xdata())
        self.ys=list(line.get_ydata())
        self.cid=line.figure.canvas.mpl_connect('button_press_event', self)
        
    def __call__(self, event):
        print 'click', event
        if event.inaxes != self.line.axes:
            print event.inaxes
            print self.line.axes
            return
        self.xs.append(event.xdata)
        self.ys.append(event.ydata)
        self.line.set_data(self.xs, self.ys)
        self.line.figure.canvas.draw()
        
fig=plt.figure()
ax=fig.add_subplot(111)
ax.set_title('click to build line segments')
line, =ax.plot([0], [0])
plt.show()
linebuilder=LineBuilder(line)

However, if I put the plt.show() on the last line, it works. Can anyone explain?

David.

plt.show is meant to raise all GUI windows and start the mainloop.
For many user interface toolkits it is a blocking call. The only
supported use is as the last line of your script.

JDH

···

On Sun, Feb 14, 2010 at 3:34 PM, David Arnold <dwarnold45@...2108...> wrote:

line, =ax.plot([0], [0])
plt.show()
linebuilder=LineBuilder(line)

However, if I put the plt.show() on the last line, it works. Can anyone explain?