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

Skip to content

Commit 184285d

Browse files
committed
Merge pull request #3419 from anntzer/bbox-repr
ENH : Better repr for Bboxes.
2 parents d78c4e9 + 212fbc3 commit 184285d

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

lib/matplotlib/tests/test_transforms.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from nose.tools import assert_equal, assert_raises
1010
import numpy.testing as np_test
11-
from numpy.testing import assert_almost_equal
11+
from numpy.testing import assert_almost_equal, assert_array_equal
1212
from matplotlib.transforms import Affine2D, BlendedGenericTransform
1313
from matplotlib.path import Path
1414
from matplotlib.scale import LogScale
@@ -450,14 +450,14 @@ def test_line_extents_for_non_affine_transData(self):
450450
expeted_data_lim)
451451

452452

453+
def assert_bbox_eq(bbox1, bbox2):
454+
assert_array_equal(bbox1.bounds, bbox2.bounds)
455+
456+
453457
def test_bbox_intersection():
454458
bbox_from_ext = mtrans.Bbox.from_extents
455459
inter = mtrans.Bbox.intersection
456460

457-
from numpy.testing import assert_array_equal as assert_a_equal
458-
def assert_bbox_eq(bbox1, bbox2):
459-
assert_a_equal(bbox1.bounds, bbox2.bounds)
460-
461461
r1 = bbox_from_ext(0, 0, 1, 1)
462462
r2 = bbox_from_ext(0.5, 0.5, 1.5, 1.5)
463463
r3 = bbox_from_ext(0.5, 0, 0.75, 0.75)
@@ -476,6 +476,18 @@ def assert_bbox_eq(bbox1, bbox2):
476476
assert_bbox_eq(inter(r1, r5), bbox_from_ext(1, 1, 1, 1))
477477

478478

479+
def test_bbox_as_strings():
480+
b = mtrans.Bbox([[.5, 0], [.75, .75]])
481+
assert_bbox_eq(b, eval(repr(b), {'Bbox': mtrans.Bbox}))
482+
asdict = eval(str(b), {'Bbox': dict})
483+
for k, v in asdict.items():
484+
assert_equal(getattr(b, k), v)
485+
fmt = '.1f'
486+
asdict = eval(format(b, fmt), {'Bbox': dict})
487+
for k, v in asdict.items():
488+
assert_equal(eval(format(getattr(b, k), fmt)), v)
489+
490+
479491
if __name__=='__main__':
480492
import nose
481493
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

lib/matplotlib/transforms.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,16 @@ def from_extents(*args):
838838
points = np.array(args, dtype=np.float_).reshape(2, 2)
839839
return Bbox(points)
840840

841+
def __format__(self, fmt):
842+
return (
843+
'Bbox(x0={0.x0:{1}}, y0={0.y0:{1}}, x1={0.x1:{1}}, y1={0.y1:{1}})'.
844+
format(self, fmt))
845+
846+
def __str__(self):
847+
return format(self, '')
848+
841849
def __repr__(self):
842-
return 'Bbox(%r)' % repr(self._points)
850+
return 'Bbox([[{0.x0}, {0.y0}], [{0.x1}, {0.y1}]])'.format(self)
843851

844852
def ignore(self, value):
845853
"""

0 commit comments

Comments
 (0)