How to regain control using mpl_disconnect() to end custom event_handling in matplotlib

后端 未结 1 951
北海茫月
北海茫月 2021-01-27 02:55

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.

相关标签:
1条回答
  • 2021-01-27 03:17
    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.

    0 讨论(0)
提交回复
热议问题