Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 461f0b0

Browse files
authored
Merge pull request matplotlib#27343 from anntzer/sfad
Fix draggable annotations on subfigures.
2 parents 4b77763 + a15f571 commit 461f0b0

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

lib/matplotlib/artist.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,8 @@ def pick(self, mouseevent):
537537
for a in self.get_children():
538538
# make sure the event happened in the same Axes
539539
ax = getattr(a, 'axes', None)
540-
if (mouseevent.inaxes is None or ax is None
540+
if (isinstance(a, mpl.figure.SubFigure)
541+
or mouseevent.inaxes is None or ax is None
541542
or mouseevent.inaxes == ax):
542543
# we need to check if mouseevent.inaxes is None
543544
# because some objects associated with an Axes (e.g., a

lib/matplotlib/offsetbox.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,7 @@ def __init__(self, ref_artist, use_blit=False):
14581458
ref_artist.set_picker(True)
14591459
self.got_artist = False
14601460
self._use_blit = use_blit and self.canvas.supports_blit
1461-
callbacks = ref_artist.figure._canvas_callbacks
1461+
callbacks = self.canvas.callbacks
14621462
self._disconnectors = [
14631463
functools.partial(
14641464
callbacks.disconnect, callbacks._connect_picklable(name, func))
@@ -1471,7 +1471,6 @@ def __init__(self, ref_artist, use_blit=False):
14711471

14721472
# A property, not an attribute, to maintain picklability.
14731473
canvas = property(lambda self: self.ref_artist.figure.canvas)
1474-
14751474
cids = property(lambda self: [
14761475
disconnect.args[0] for disconnect in self._disconnectors[:2]])
14771476

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,13 @@ def test_remove_draggable():
450450
an.draggable(True)
451451
an.remove()
452452
MouseEvent("button_release_event", fig.canvas, 1, 1)._process()
453+
454+
455+
def test_draggable_in_subfigure():
456+
fig = plt.figure()
457+
# Put annotation at lower left corner to make it easily pickable below.
458+
ann = fig.subfigures().add_axes([0, 0, 1, 1]).annotate("foo", (0, 0))
459+
ann.draggable(True)
460+
fig.canvas.draw() # Texts are non-pickable until the first draw.
461+
MouseEvent("button_press_event", fig.canvas, 1, 1)._process()
462+
assert ann._draggable.got_artist

0 commit comments

Comments
 (0)