Skip to content

Instantly share code, notes, and snippets.

@erikaris
Last active December 1, 2017 15:32
Show Gist options
  • Save erikaris/dad705cac1c110104b65d05bbd7cb0df to your computer and use it in GitHub Desktop.
Save erikaris/dad705cac1c110104b65d05bbd7cb0df to your computer and use it in GitHub Desktop.
# taken from: https://stackoverflow.com/questions/5600370/python-matplotlib-add-and-remove-text-to-figure-using-button-events#answer-5600964
txt = None
def onclick(event):
global txt # why global txt? because there's a variable assignment
txt = plt.text(event.xdata, event.ydata, 'TESTTEST', fontsize=8)
fig.canvas.draw()
def offclick(event):
txt.remove() # why there's no global txt? because there's no variable assignment
fig.canvas.draw()
fig.canvas.mpl_connect('button_press_event', onclick)
fig.canvas.mpl_connect('button_release_event', offclick)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment