diff --git a/doc/api/next_api_changes/behavior/22485-TH.rst b/doc/api/next_api_changes/behavior/22485-TH.rst new file mode 100644 index 000000000000..b2d7f61238bd --- /dev/null +++ b/doc/api/next_api_changes/behavior/22485-TH.rst @@ -0,0 +1,5 @@ +``AxesImage`` string representation +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +The string representation of `.AxesImage` changes from +stating the position in the figure ``"AxesImage(80,52.8;496x369.6)"`` to +giving the number of pixels ``"AxesImage(size=(300, 200))"``. diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index ed4f70b39d0e..a7642a2cb21e 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -275,6 +275,13 @@ def __init__(self, ax, self._internal_update(kwargs) + def __str__(self): + try: + size = self.get_size() + return f"{type(self).__name__}(size={size!r})" + except RuntimeError: + return type(self).__name__ + def __getstate__(self): # Save some space on the pickle by not saving the cache. return {**super().__getstate__(), "_imcache": None} @@ -896,8 +903,6 @@ class AxesImage(_ImageBase): the output image is larger than the input image. **kwargs : `.Artist` properties """ - def __str__(self): - return "AxesImage(%g,%g;%gx%g)" % tuple(self.axes.bbox.bounds) def __init__(self, ax, cmap=None,