Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c98c4b8

Browse files
committed
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.
1 parent 5c8cf78 commit c98c4b8

File tree

2 files changed

+13
-21
lines changed

2 files changed

+13
-21
lines changed

lib/matplotlib/tests/test_transforms.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -508,14 +508,8 @@ def test_str_transform():
508508
IdentityTransform(),
509509
IdentityTransform())),
510510
CompositeAffine2D(
511-
Affine2D(
512-
[[1. 0. 0.]
513-
[0. 1. 0.]
514-
[0. 0. 1.]]),
515-
Affine2D(
516-
[[1. 0. 0.]
517-
[0. 1. 0.]
518-
[0. 0. 1.]]))),
511+
Affine2D().scale(1.0),
512+
Affine2D().scale(1.0))),
519513
PolarTransform(
520514
PolarAxesSubplot(0.125,0.1;0.775x0.8),
521515
use_rmin=True,
@@ -537,14 +531,8 @@ def test_str_transform():
537531
TransformedBbox(
538532
Bbox(x0=0.0, y0=0.0, x1=6.283185307179586, y1=1.0),
539533
CompositeAffine2D(
540-
Affine2D(
541-
[[1. 0. 0.]
542-
[0. 1. 0.]
543-
[0. 0. 1.]]),
544-
Affine2D(
545-
[[1. 0. 0.]
546-
[0. 1. 0.]
547-
[0. 0. 1.]]))),
534+
Affine2D().scale(1.0),
535+
Affine2D().scale(1.0))),
548536
LockableBbox(
549537
Bbox(x0=0.0, y0=0.0, x1=6.283185307179586, y1=1.0),
550538
[[-- --]
@@ -555,10 +543,7 @@ def test_str_transform():
555543
BboxTransformTo(
556544
TransformedBbox(
557545
Bbox(x0=0.0, y0=0.0, x1=8.0, y1=6.0),
558-
Affine2D(
559-
[[80. 0. 0.]
560-
[ 0. 80. 0.]
561-
[ 0. 0. 1.]])))))))"""
546+
Affine2D().scale(80.0)))))))"""
562547

563548

564549
def test_transform_single_point():

lib/matplotlib/transforms.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1887,7 +1887,14 @@ def __init__(self, matrix=None, **kwargs):
18871887
self._mtx = matrix.copy()
18881888
self._invalid = 0
18891889

1890-
__str__ = _make_str_method("_mtx")
1890+
_base_str = _make_str_method("_mtx")
1891+
1892+
def __str__(self):
1893+
return (self._base_str()
1894+
if (self._mtx != np.diag(np.diag(self._mtx))).any()
1895+
else f"Affine2D().scale({self._mtx[0, 0]}, {self._mtx[1, 1]})"
1896+
if self._mtx[0, 0] != self._mtx[1, 1]
1897+
else f"Affine2D().scale({self._mtx[0, 0]})")
18911898

18921899
@staticmethod
18931900
def from_values(a, b, c, d, e, f):

0 commit comments

Comments
 (0)