Closed
Description
mpl1.5.1/2.0b2
A minimal mpldatacursor-like module, that adds a single annotation wherever the user left-clicks and removes it upon right click:
from matplotlib import pyplot as plt
fig, ax = plt.subplots()
ann = None
def cb(event):
global ann
if event.button == 1:
if ann:
ann.remove()
ann = ax.annotate("foo", (event.xdata, event.ydata))
fig.canvas.draw()
if event.button == 3:
if ann:
ann.remove()
ann = None
fig.canvas.draw()
fig.canvas.mpl_connect("button_press_event", cb)
plt.show()
If we want to make the annotation also draggable, we could add ann.draggable()
after adding it. But then right-clicking to remove the annotation results in
Traceback (most recent call last):
File "/usr/lib/python3.5/site-packages/matplotlib/backends/backend_qt5.py", line 290, in mouseReleaseEvent
guiEvent=event)
File "/usr/lib/python3.5/site-packages/matplotlib/backend_bases.py", line 1921, in button_release_event
self.callbacks.process(s, event)
File "/usr/lib/python3.5/site-packages/matplotlib/cbook.py", line 550, in process
proxy(*args, **kwargs)
File "/usr/lib/python3.5/site-packages/matplotlib/cbook.py", line 417, in __call__
return mtd(*args, **kwargs)
File "/usr/lib/python3.5/site-packages/matplotlib/offsetbox.py", line 1687, in on_release
self.finalize_offset()
File "/usr/lib/python3.5/site-packages/matplotlib/offsetbox.py", line 1768, in finalize_offset
pos_axes_fraction = self.annotation.axes.transAxes.inverted()
AttributeError: 'NoneType' object has no attribute 'transAxes'
Fatal Python error: Aborted
basically because the button_release
callback is called one last time after the annotation has been removed from the axes (and thus annotation.axes
is None
).
So either the draggable artist should check that it is still valid while executing callback code, or it should disconnect itself upon removal (but there may be some weird race conditions?).
Metadata
Metadata
Assignees
Labels
No labels