Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b6c6d6c

Browse filesBrowse files
authored
Merge pull request #13708 from anntzer/annotation-contains
Annotation.contains shouldn't consider the text+arrow's joint bbox.
2 parents 474f6c5 + c372df2 commit b6c6d6c
Copy full SHA for b6c6d6c

File tree

Expand file treeCollapse file tree

2 files changed

+21
-6
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+21
-6
lines changed

‎lib/matplotlib/tests/test_text.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_text.py
+15-4Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pytest
77

88
import matplotlib
9+
from matplotlib.backend_bases import MouseEvent
910
import matplotlib.patches as mpatches
1011
import matplotlib.pyplot as plt
1112
from matplotlib.testing.decorators import image_comparison
@@ -204,13 +205,10 @@ def test_afm_kerning():
204205

205206
@image_comparison(baseline_images=['text_contains'], extensions=['png'])
206207
def test_contains():
207-
import matplotlib.backend_bases as mbackend
208-
209208
fig = plt.figure()
210209
ax = plt.axes()
211210

212-
mevent = mbackend.MouseEvent(
213-
'button_press_event', fig.canvas, 0.5, 0.5, 1, None)
211+
mevent = MouseEvent('button_press_event', fig.canvas, 0.5, 0.5, 1, None)
214212

215213
xs = np.linspace(0.25, 0.75, 30)
216214
ys = np.linspace(0.25, 0.75, 30)
@@ -236,6 +234,19 @@ def test_contains():
236234
ax.viewLim.set(vl)
237235

238236

237+
def test_annotation_contains():
238+
# Check that Annotation.contains looks at the bboxes of the text and the
239+
# arrow separately, not at the joint bbox.
240+
fig, ax = plt.subplots()
241+
ann = ax.annotate(
242+
"hello", xy=(.4, .4), xytext=(.6, .6), arrowprops={"arrowstyle": "->"})
243+
fig.canvas.draw() # Needed for the same reason as in test_contains.
244+
event = MouseEvent(
245+
"button_press_event", fig.canvas,
246+
*ax.transData.transform_point((.5, .6)))
247+
assert ann.contains(event) == (False, {})
248+
249+
239250
@image_comparison(baseline_images=['titles'])
240251
def test_titles():
241252
# left and right side titles

‎lib/matplotlib/text.py

Copy file name to clipboardExpand all lines: lib/matplotlib/text.py
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,10 @@ def contains(self, mouseevent):
200200
if not self.get_visible() or self._renderer is None:
201201
return False, {}
202202

203-
l, b, w, h = self.get_window_extent().bounds
203+
# Explicitly use Text.get_window_extent(self) and not
204+
# self.get_window_extent() so that Annotation.contains does not
205+
# accidentally cover the entire annotation bounding box.
206+
l, b, w, h = Text.get_window_extent(self).bounds
204207
r, t = l + w, b + h
205208

206209
x, y = mouseevent.x, mouseevent.y
@@ -2187,11 +2190,12 @@ def transform(renderer) -> Transform
21872190
self.arrow_patch = None
21882191

21892192
def contains(self, event):
2193+
if self._contains is not None:
2194+
return self._contains(self, event)
21902195
contains, tinfo = Text.contains(self, event)
21912196
if self.arrow_patch is not None:
21922197
in_patch, _ = self.arrow_patch.contains(event)
21932198
contains = contains or in_patch
2194-
21952199
return contains, tinfo
21962200

21972201
@property

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.