Skip to content

Instantly share code, notes, and snippets.

@erikaris
Last active December 3, 2017 12:54
Show Gist options
  • Select an option

  • Save erikaris/3b96a522d2c0406676d83fa30c0bacca to your computer and use it in GitHub Desktop.

Select an option

Save erikaris/3b96a522d2c0406676d83fa30c0bacca to your computer and use it in GitHub Desktop.
# modified from: http://matplotlib.org/users/event_handling.html?highlight=event%20picking#event-attributes
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())
# cid = connection id
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 = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('click to build line segments')
line, = ax.plot([0], [0]) # empty plot; the position of [0, 0] will be in the middle of the plot
linebuilder = LineBuilder(line)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment