From c98c4b84f3517ffc94a53899f9005df56e5dbb3e Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 20 Apr 2021 23:34:32 +0200 Subject: [PATCH] Shorten the repr of scaling transforms. Scaling transforms (with just nonzero diagonal terms) are quite common, and special-casing them makes many transform reprs shorter, helping with debugging. See e.g. changes in the test_transforms reference output. --- lib/matplotlib/tests/test_transforms.py | 25 +++++-------------------- lib/matplotlib/transforms.py | 9 ++++++++- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/lib/matplotlib/tests/test_transforms.py b/lib/matplotlib/tests/test_transforms.py index d073e8c13dea..a31e8de9951a 100644 --- a/lib/matplotlib/tests/test_transforms.py +++ b/lib/matplotlib/tests/test_transforms.py @@ -508,14 +508,8 @@ def test_str_transform(): IdentityTransform(), IdentityTransform())), CompositeAffine2D( - Affine2D( - [[1. 0. 0.] - [0. 1. 0.] - [0. 0. 1.]]), - Affine2D( - [[1. 0. 0.] - [0. 1. 0.] - [0. 0. 1.]]))), + Affine2D().scale(1.0), + Affine2D().scale(1.0))), PolarTransform( PolarAxesSubplot(0.125,0.1;0.775x0.8), use_rmin=True, @@ -537,14 +531,8 @@ def test_str_transform(): TransformedBbox( Bbox(x0=0.0, y0=0.0, x1=6.283185307179586, y1=1.0), CompositeAffine2D( - Affine2D( - [[1. 0. 0.] - [0. 1. 0.] - [0. 0. 1.]]), - Affine2D( - [[1. 0. 0.] - [0. 1. 0.] - [0. 0. 1.]]))), + Affine2D().scale(1.0), + Affine2D().scale(1.0))), LockableBbox( Bbox(x0=0.0, y0=0.0, x1=6.283185307179586, y1=1.0), [[-- --] @@ -555,10 +543,7 @@ def test_str_transform(): BboxTransformTo( TransformedBbox( Bbox(x0=0.0, y0=0.0, x1=8.0, y1=6.0), - Affine2D( - [[80. 0. 0.] - [ 0. 80. 0.] - [ 0. 0. 1.]])))))))""" + Affine2D().scale(80.0)))))))""" def test_transform_single_point(): diff --git a/lib/matplotlib/transforms.py b/lib/matplotlib/transforms.py index e2051a41346b..7866cf46fb56 100644 --- a/lib/matplotlib/transforms.py +++ b/lib/matplotlib/transforms.py @@ -1887,7 +1887,14 @@ def __init__(self, matrix=None, **kwargs): self._mtx = matrix.copy() self._invalid = 0 - __str__ = _make_str_method("_mtx") + _base_str = _make_str_method("_mtx") + + def __str__(self): + return (self._base_str() + if (self._mtx != np.diag(np.diag(self._mtx))).any() + else f"Affine2D().scale({self._mtx[0, 0]}, {self._mtx[1, 1]})" + if self._mtx[0, 0] != self._mtx[1, 1] + else f"Affine2D().scale({self._mtx[0, 0]})") @staticmethod def from_values(a, b, c, d, e, f):