8000 Merge pull request #26101 from QuLogic/text-marker-centre · matplotlib/matplotlib@5f29763 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f29763

Browse files
authored
Merge pull request #26101 from QuLogic/text-marker-centre
Correct bounding box calculation for text markers
2 parents 5258ffc + e397ae7 commit 5f29763

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

lib/matplotlib/markers.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,12 @@ def _set_mathtext_path(self):
509509
if len(text.vertices) == 0:
510510
return
511511

512-
xmin, ymin = text.vertices.min(axis=0)
513-
xmax, ymax = text.vertices.max(axis=0)
514-
width = xmax - xmin
515-
height = ymax - ymin
516-
max_dim = max(width, height)
517-
self._transform = Affine2D() \
518-
.translate(-xmin + 0.5 * -width, -ymin + 0.5 * -height) \
519-
.scale(1.0 / max_dim)
512+
bbox = text.get_extents()
513+
max_dim = max(bbox.width, bbox.height)
514+
self._transform = (
515+
Affine2D()
516+
.translate(-bbox.xmin + 0.5 * -bbox.width, -bbox.ymin + 0.5 * -bbox.height)
517+
.scale(1.0 / max_dim))
520518
self._path = text
521519
self._snap = False
522520

lib/matplotlib/tests/test_marker.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ def draw_ref_marker(y, style, size):
156156
ax_ref.set(xlim=(-0.5, 1.5), ylim=(-0.5, 1.5))
157157

158158

159+
# The bullet mathtext marker is not quite a circle, so this is not a perfect match, but
160+
# it is close enough to confirm that the text-based marker is centred correctly. But we
161+
# still need a small tolerance to work around that difference.
162+
@check_figures_equal(extensions=['png'], tol=1.86)
163+
def test_text_marker(fig_ref, fig_test):
164+
ax_ref = fig_ref.add_subplot()
165+
ax_test = fig_test.add_subplot()
166+
167+
ax_ref.plot(0, 0, marker=r'o', markersize=100, markeredgewidth=0)
168+
ax_test.plot(0, 0, marker=r'$\bullet$', markersize=100, markeredgewidth=0)
169+
170+
159171
@check_figures_equal()
160172
def test_marker_clipping(fig_ref, fig_test):
161173
# Plotting multiple markers can trigger different optimized paths in

0 commit comments

Comments
 (0)
0