Allows to draw lines on plot

PHOTO EMBED

Sun Apr 04 2021 02:24:29 GMT+0000 (Coordinated Universal Time)

Saved by @marcoycaza #python

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', event)
        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()

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

plt.show()
content_copyCOPY

I've not tested this fully.

https://gist.github.com/MarcoYcaza/46e0d07a0a02074e50dd0c6c87f5d79f