Skip to content Skip to sidebar Skip to footer

How To Regain Control Using Mpl_disconnect() To End Custom Event_handling In Matplotlib

I want the use the mpl_disconnect() function to regain control when some GUI is finished getting input. I cannot get mpl_disconnect() to work in any situation I've tried. For illus

Solution 1:

from matplotlib import 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', vars(event))
        if event.button==3:
            print('clean up')
            event.canvas.mpl_disconnect(self.cid)
            return
        if event.inaxes != 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_idle()

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click to build line segments')
line, = ax.plot([0], [0])  # empty line
linebuilder = LineBuilder(line)

plt.show()

works as expected for me.


Post a Comment for "How To Regain Control Using Mpl_disconnect() To End Custom Event_handling In Matplotlib"