From 9732a3971f1da458cf832775004ad92acc1abd34 Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Fri, 19 May 2023 16:26:26 -0500 Subject: [PATCH] Backport PR #25920: Rewrite offset_copy for better error message --- lib/matplotlib/tests/test_transforms.py | 11 +++++++++++ lib/matplotlib/transforms.py | 6 ++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/tests/test_transforms.py b/lib/matplotlib/tests/test_transforms.py index 064a7240c39d..85df46b4c9d0 100644 --- a/lib/matplotlib/tests/test_transforms.py +++ b/lib/matplotlib/tests/test_transforms.py @@ -744,3 +744,14 @@ def test_scale_swapping(fig_test, fig_ref): ax.plot(x, np.exp(-(x**2) / 2) / np.sqrt(2 * np.pi)) fig.canvas.draw() ax.set_yscale('linear') + + +def test_offset_copy_errors(): + with pytest.raises(ValueError, + match="'fontsize' is not a valid value for units;" + " supported values are 'dots', 'points', 'inches'"): + mtransforms.offset_copy(None, units='fontsize') + + with pytest.raises(ValueError, + match='For units of inches or points a fig kwarg is needed'): + mtransforms.offset_copy(None, units='inches') diff --git a/lib/matplotlib/transforms.py b/lib/matplotlib/transforms.py index c5c051be570f..e3f2f28da78d 100644 --- a/lib/matplotlib/transforms.py +++ b/lib/matplotlib/transforms.py @@ -2968,6 +2968,7 @@ def offset_copy(trans, fig=None, x=0.0, y=0.0, units='inches'): `Transform` subclass Transform with applied offset. """ + _api.check_in_list(['dots', 'points', 'inches'], units=units) if units == 'dots': return trans + Affine2D().translate(x, y) if fig is None: @@ -2975,8 +2976,5 @@ def offset_copy(trans, fig=None, x=0.0, y=0.0, units='inches'): if units == 'points': x /= 72.0 y /= 72.0 - elif units == 'inches': - pass - else: - _api.check_in_list(['dots', 'points', 'inches'], units=units) + # Default units are 'inches' return trans + ScaledTranslation(x, y, fig.dpi_scale_trans)