From e397ae79945ab80c693482fabbb2ee122de4da47 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Fri, 9 Jun 2023 22:32:13 -0400 Subject: [PATCH] Correct bounding box calculation for text markers Points with `CLOSEPOLY` or `STOP` codes have vertex values that are meaningless, but the marker bounding box calculation still counts them. This causes several markers to not be correctly centred as expected. Fixes #26083 --- lib/matplotlib/markers.py | 14 ++++++-------- lib/matplotlib/tests/test_marker.py | 12 ++++++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/markers.py b/lib/matplotlib/markers.py index eb689ef661c7..e9bf7c02fb1f 100644 --- a/lib/matplotlib/markers.py +++ b/lib/matplotlib/markers.py @@ -509,14 +509,12 @@ def _set_mathtext_path(self): if len(text.vertices) == 0: return - xmin, ymin = text.vertices.min(axis=0) - xmax, ymax = text.vertices.max(axis=0) - width = xmax - xmin - height = ymax - ymin - max_dim = max(width, height) - self._transform = Affine2D() \ - .translate(-xmin + 0.5 * -width, -ymin + 0.5 * -height) \ - .scale(1.0 / max_dim) + bbox = text.get_extents() + max_dim = max(bbox.width, bbox.height) + self._transform = ( + Affine2D() + .translate(-bbox.xmin + 0.5 * -bbox.width, -bbox.ymin + 0.5 * -bbox.height) + .scale(1.0 / max_dim)) self._path = text self._snap = False diff --git a/lib/matplotlib/tests/test_marker.py b/lib/matplotlib/tests/test_marker.py index 13f977dc38f1..463ff1d05c96 100644 --- a/lib/matplotlib/tests/test_marker.py +++ b/lib/matplotlib/tests/test_marker.py @@ -156,6 +156,18 @@ def draw_ref_marker(y, style, size): ax_ref.set(xlim=(-0.5, 1.5), ylim=(-0.5, 1.5)) +# The bullet mathtext marker is not quite a circle, so this is not a perfect match, but +# it is close enough to confirm that the text-based marker is centred correctly. But we +# still need a small tolerance to work around that difference. +@check_figures_equal(extensions=['png'], tol=1.86) +def test_text_marker(fig_ref, fig_test): + ax_ref = fig_ref.add_subplot() + ax_test = fig_test.add_subplot() + + ax_ref.plot(0, 0, marker=r'o', markersize=100, markeredgewidth=0) + ax_test.plot(0, 0, marker=r'$\bullet$', markersize=100, markeredgewidth=0) + + @check_figures_equal() def test_marker_clipping(fig_ref, fig_test): # Plotting multiple markers can trigger different optimized paths in