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

Skip to content

Commit 8afcbc2

Browse files
authored
Merge pull request #23452 from scottshambaugh/axes3d_repr
Generalize Axes __repr__ to 3D
2 parents 11a3e1b + c419cef commit 8afcbc2

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -732,10 +732,9 @@ def __repr__(self):
732732
titles.append(f"{k!r}:{title!r}")
733733
if titles:
734734
fields += ["title={" + ",".join(titles) + "}"]
735-
if self.get_xlabel():
736-
fields += [f"xlabel={self.get_xlabel()!r}"]
737-
if self.get_ylabel():
738-
fields += [f"ylabel={self.get_ylabel()!r}"]
735+
for name, axis in self._axis_map.items():
736+
if axis.get_label() and axis.get_label().get_text():
737+
fields += [f"{name}label={axis.get_label().get_text()!r}"]
739738
return f"<{self.__class__.__name__}:" + ", ".join(fields) + ">"
740739

741740
@_api.delete_parameter("3.6", "args")

lib/matplotlib/tests/test_axes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ def test_get_labels():
5959
assert ax.get_ylabel() == 'y label'
6060

6161

62+
def test_repr():
63+
fig, ax = plt.subplots()
64+
ax.set_label('label')
65+
ax.set_title('title')
66+
ax.set_xlabel('x')
67+
ax.set_ylabel('y')
68+
assert repr(ax) == ("<AxesSubplot:label='label', " +
69+
"title={'center':'title'}, xlabel='x', ylabel='y'>")
70+
71+
6272
@check_figures_equal()
6373
def test_label_loc_vertical(fig_test, fig_ref):
6474
ax = fig_test.subplots()

lib/mpl_toolkits/tests/test_mplot3d.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ def test_aspect_equal_error():
3434
ax.set_aspect('equal')
3535

3636

37+
def test_axes3d_repr():
38+
fig = plt.figure()
39+
ax = fig.add_subplot(projection='3d')
40+
ax.set_label('label')
41+
ax.set_title('title')
42+
ax.set_xlabel('x')
43+
ax.set_ylabel('y')
44+
ax.set_zlabel('z')
45+
assert repr(ax) == ("<Axes3DSubplot:label='label', " +
46+
"title={'center':'title'}, " +
47+
"xlabel='x', ylabel='y', zlabel='z'>")
48+
49+
3750
@mpl3d_image_comparison(['bar3d.png'])
3851
def test_bar3d():
3952
fig = plt.figure()

0 commit comments

Comments
 (0)