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

Skip to content

Commit 069fea5

Browse files
authored
Merge pull request #17002 from QuLogic/fix-annotationbbox-zoom
Fix AnnotationBbox picking and a bit of cleanup
2 parents 1f48038 + 1816036 commit 069fea5

File tree

2 files changed

+63
-21
lines changed

2 files changed

+63
-21
lines changed

lib/matplotlib/offsetbox.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,24 +1344,6 @@ def set_zoom(self, zoom):
13441344
def get_zoom(self):
13451345
return self._zoom
13461346

1347-
# def set_axes(self, axes):
1348-
# self.image.set_axes(axes)
1349-
# martist.Artist.set_axes(self, axes)
1350-
1351-
# def set_offset(self, xy):
1352-
# """
1353-
# Set the offset of the container.
1354-
#
1355-
# Parameters
1356-
# ----------
1357-
# xy : (float, float)
1358-
# The (x, y) coordinates of the offset in display units.
1359-
# """
1360-
# self._offset = xy
1361-
1362-
# self.offset_transform.clear()
1363-
# self.offset_transform.translate(xy[0], xy[1])
1364-
13651347
def get_offset(self):
13661348
"""Return offset of the container."""
13671349
return self._offset
@@ -1432,7 +1414,7 @@ def __init__(self, offsetbox, xy,
14321414
14331415
xybox : (float, float), default: *xy*
14341416
The position *(x, y)* to place the text at. The coordinate system
1435-
is determined by *textcoords*.
1417+
is determined by *boxcoords*.
14361418
14371419
xycoords : str or `.Artist` or `.Transform` or callable or \
14381420
(float, float), default: 'data'
@@ -1525,6 +1507,11 @@ def contains(self, mouseevent):
15251507
inside, info = self._default_contains(mouseevent)
15261508
if inside is not None:
15271509
return inside, info
1510+
1511+
xy_pixel = self._get_position_xy(None)
1512+
if not self._check_xy(None, xy_pixel):
1513+
return False, {}
1514+
15281515
t, tinfo = self.offsetbox.contains(mouseevent)
15291516
#if self.arrow_patch is not None:
15301517
# a, ainfo=self.arrow_patch.contains(event)
@@ -1643,7 +1630,6 @@ def draw(self, renderer):
16431630
return
16441631

16451632
xy_pixel = self._get_position_xy(renderer)
1646-
16471633
if not self._check_xy(renderer, xy_pixel):
16481634
return
16491635

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
from collections import namedtuple
22

3+
import numpy as np
34
from numpy.testing import assert_allclose
45
import pytest
56

67
from matplotlib.testing.decorators import image_comparison
78
import matplotlib.pyplot as plt
89
import matplotlib.patches as mpatches
910
import matplotlib.lines as mlines
11+
from matplotlib.backend_bases import MouseButton
12+
1013
from matplotlib.offsetbox import (
11-
AnchoredOffsetbox, DrawingArea, _get_packed_offsets)
14+
AnchoredOffsetbox, AnnotationBbox, DrawingArea, OffsetImage, TextArea,
15+
_get_packed_offsets)
1216

1317

1418
@image_comparison(['offsetbox_clipping'], remove_text=True)
@@ -182,3 +186,55 @@ def test_get_packed_offsets_equal(wd_list, total, sep, expected):
182186
def test_get_packed_offsets_equal_total_none_sep_none():
183187
with pytest.raises(ValueError):
184188
_get_packed_offsets([(1, 0)] * 3, total=None, sep=None, mode='equal')
189+
190+
191+
@pytest.mark.parametrize('child_type', ['draw', 'image', 'text'])
192+
@pytest.mark.parametrize('boxcoords',
193+
['axes fraction', 'axes pixels', 'axes points',
194+
'data'])
195+
def test_picking(child_type, boxcoords):
196+
# These all take up approximately the same area.
197+
if child_type == 'draw':
198+
picking_child = DrawingArea(5, 5)
199+
picking_child.add_artist(mpatches.Rectangle((0, 0), 5, 5, linewidth=0))
200+
elif child_type == 'image':
201+
im = np.ones((5, 5))
202+
im[2, 2] = 0
203+
picking_child = OffsetImage(im)
204+
elif child_type == 'text':
205+
picking_child = TextArea('\N{Black Square}', textprops={'fontsize': 5})
206+
else:
207+
assert False, f'Unknown picking child type {child_type}'
208+
209+
fig, ax = plt.subplots()
210+
ab = AnnotationBbox(picking_child, (0.5, 0.5), boxcoords=boxcoords)
211+
ab.set_picker(True)
212+
ax.add_artist(ab)
213+
214+
calls = []
215+
fig.canvas.mpl_connect('pick_event', lambda event: calls.append(event))
216+
217+
# Annotation should be picked by an event occurring at its center.
218+
if boxcoords == 'axes points':
219+
x, y = ax.transAxes.transform_point((0, 0))
220+
x += 0.5 * fig.dpi / 72
221+
y += 0.5 * fig.dpi / 72
222+
elif boxcoords == 'axes pixels':
223+
x, y = ax.transAxes.transform_point((0, 0))
224+
x += 0.5
225+
y += 0.5
226+
else:
227+
x, y = ax.transAxes.transform_point((0.5, 0.5))
228+
fig.canvas.draw()
229+
calls.clear()
230+
fig.canvas.button_press_event(x, y, MouseButton.LEFT)
231+
assert len(calls) == 1 and calls[0].artist == ab
232+
233+
# Annotation should *not* be picked by an event at its original center
234+
# point when the limits have changed enough to hide the *xy* point.
235+
ax.set_xlim(-1, 0)
236+
ax.set_ylim(-1, 0)
237+
fig.canvas.draw()
238+
calls.clear()
239+
fig.canvas.button_press_event(x, y, MouseButton.LEFT)
240+
assert len(calls) == 0

0 commit comments

Comments
 (0)