From 3adaf0290fe2417a96c45ef408e759edebcef294 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Thu, 17 Feb 2022 22:39:15 +0100 Subject: [PATCH] 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. --- doc/api/next_api_changes/behavior/22485-TH.rst | 5 +++++ lib/matplotlib/image.py | 9 +++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 doc/api/next_api_changes/behavior/22485-TH.rst 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,