From 51a9f161dfb10540f9f93cb0708be49574a9f14f Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 21 Jul 2022 21:07:02 +0200 Subject: [PATCH] Tweak Axes repr. ... by putting spaces where expected in normal style. Also slightly refactor the implementation. --- lib/matplotlib/axes/_base.py | 14 +++++++------- lib/matplotlib/tests/test_axes.py | 7 ++++--- lib/mpl_toolkits/tests/test_mplot3d.py | 6 +++--- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 5717b1f1d17f..7f917ab6cbdd 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -724,18 +724,18 @@ def __repr__(self): fields = [] if self.get_label(): fields += [f"label={self.get_label()!r}"] - titles = [] - for k in ["left", "center", "right"]: - if hasattr(self, 'get_title'): + if hasattr(self, "get_title"): + titles = {} + for k in ["left", "center", "right"]: title = self.get_title(loc=k) if title: - titles.append(f"{k!r}:{title!r}") - if titles: - fields += ["title={" + ",".join(titles) + "}"] + titles[k] = title + if titles: + fields += [f"title={titles}"] for name, axis in self._axis_map.items(): if axis.get_label() and axis.get_label().get_text(): fields += [f"{name}label={axis.get_label().get_text()!r}"] - return f"<{self.__class__.__name__}:" + ", ".join(fields) + ">" + return f"<{self.__class__.__name__}: " + ", ".join(fields) + ">" @_api.delete_parameter("3.6", "args") @_api.delete_parameter("3.6", "kwargs") diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 329af83ab873..721e35768265 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -65,8 +65,9 @@ def test_repr(): ax.set_title('title') ax.set_xlabel('x') ax.set_ylabel('y') - assert repr(ax) == ("") + assert repr(ax) == ( + "") @check_figures_equal() @@ -7046,7 +7047,7 @@ def test_secondary_formatter(): def test_secondary_repr(): fig, ax = plt.subplots() secax = ax.secondary_xaxis("top") - assert repr(secax) == '' + assert repr(secax) == '' def color_boxes(fig, ax): diff --git a/lib/mpl_toolkits/tests/test_mplot3d.py b/lib/mpl_toolkits/tests/test_mplot3d.py index 7df8fa3ac2d8..1024444df2ca 100644 --- a/lib/mpl_toolkits/tests/test_mplot3d.py +++ b/lib/mpl_toolkits/tests/test_mplot3d.py @@ -42,9 +42,9 @@ def test_axes3d_repr(): ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('z') - assert repr(ax) == ("") + assert repr(ax) == ( + "") @mpl3d_image_comparison(['bar3d.png'])