Skip to content

Commit 3adaf02

Browse files
committed
Change string representation of AxesImage
- The current implementation was unhelpful / incorrect: It returned the axes bounding box: Fixes #20294 - The string should contain helpful information. I argue that the figure coordinates of the image are not helpful most of the time. Instead, giving the number of pixels seems more useful. Note that __str__ does not need to be unique. - Also move the definition to the base class and make it type-agnostic so that it works correctly for all derived classes.
1 parent c2f39dd commit 3adaf02

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``AxesImage`` string representation
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
The string representation of `.AxesImage` changes from
4+
stating the position in the figure ``"AxesImage(80,52.8;496x369.6)"`` to
5+
giving the number of pixels ``"AxesImage(size=(300, 200))"``.

lib/matplotlib/image.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ def __init__(self, ax,
275275

276276
self._internal_update(kwargs)
277277

278+
def __str__(self):
279+
try:
280+
size = self.get_size()
281+
return f"{type(self).__name__}(size={size!r})"
282+
except RuntimeError:
283+
return type(self).__name__
284+
278285
def __getstate__(self):
279286
# Save some space on the pickle by not saving the cache.
280287
return {**super().__getstate__(), "_imcache": None}
@@ -896,8 +903,6 @@ class AxesImage(_ImageBase):
896903
the output image is larger than the input image.
897904
**kwargs : `.Artist` properties
898905
"""
899-
def __str__(self):
900-
return "AxesImage(%g,%g;%gx%g)" % tuple(self.axes.bbox.bounds)
901906

902907
def __init__(self, ax,
903908
cmap=None,

0 commit comments

Comments
 (0)