8000 Backport PR #22283: Fixed `repr` for `SecondaryAxis · matplotlib/matplotlib@ccd0f1b · GitHub
[go: up one dir, main page]

Skip to content

Commit ccd0f1b

Browse files
greglucasoscargus
authored andcommitted
Backport PR #22283: Fixed repr for `SecondaryAxis
1 parent 74977a8 commit ccd0f1b

File tree

2 files changed

+112
-3
lines changed

2 files changed

+112
-3
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -710,9 +710,10 @@ def __repr__(self):
710710
fields += [f"label={self.get_label()!r}"]
711711
titles = []
712712
for k in ["left", "center", "right"]:
713-
title = self.get_title(loc=k)
714-
if title:
715-
titles.append(f"{k!r}:{title!r}")
713+
if hasattr(self, 'get_title'):
714+
title = self.get_title(loc=k)
715+
if title:
716+
titles.append(f"{k!r}:{title!r}")
716717
if titles:
717718
fields += ["title={" + ",".join(titles) + "}"]
718719
if self.get_xlabel():

lib/matplotlib/tests/test_axes.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,20 @@ def test_autoscale_tight():
488488
assert_allclose(ax.get_xlim(), (-0.15, 3.15))
489489
assert_allclose(ax.get_ylim(), (1.0, 4.0))
490490

491+
# Check that autoscale is on
492+
assert ax.get_autoscalex_on()
493+
assert ax.get_autoscaley_on()
494+
assert ax.get_autoscale_on()
495+
# Set enable to None
496+
ax.autoscale(enable=None)
497+
# Same limits
498+
assert_allclose(ax.get_xlim(), (-0.15, 3.15))
499+
assert_allclose(ax.get_ylim(), (1.0, 4.0))
500+
# autoscale still on
501+
assert ax.get_autoscalex_on()
502+
assert ax.get_autoscaley_on()
503+
assert ax.get_autoscale_on()
504+
491505

492506
@mpl.style.context('default')
493507
def test_autoscale_log_shared():
@@ -4947,6 +4961,30 @@ def test_shared_with_aspect_3():
49474961
assert round(expected, 4) == round(ax.get_aspect(), 4)
49484962

49494963

4964+
def test_shared_aspect_error():
4965+
fig, axes = plt.subplots(1, 2, sharex=True, sharey=True)
4966+
axes[0].axis("equal")
4967+
with pytest.raises(RuntimeError, match=r"set_aspect\(..., adjustable="):
4968+
fig.draw_without_rendering()
4969+
4970+
4971+
@pytest.mark.parametrize('err, args, kwargs, match',
4972+
((TypeError, (1, 2), {},
4973+
r"axis\(\) takes 0 or 1 positional arguments but 2"
4974+
" were given"),
4975+
(ValueError, ('foo', ), {},
4976+
"Unrecognized string foo to axis; try on or off"),
4977+
(TypeError, ([1, 2], ), {},
4978+
"the first argument to axis*"),
4979+
(TypeError, tuple(), {'foo': None},
4980+
r"axis\(\) got an unexpected keyword argument "
4981+
"'foo'"),
4982+
))
4983+
def test_axis_errors(err, args, kwargs, match):
4984+
with pytest.raises(err, match=match):
4985+
plt.axis(*args, **kwargs)
4986+
4987+
49504988
@pytest.mark.parametrize('twin', ('x', 'y'))
49514989
def test_twin_with_aspect(twin):
49524990
fig, ax = plt.subplots()
@@ -5340,12 +5378,58 @@ def test_set_margin_updates_limits():
53405378
assert ax.get_xlim() == (1, 2)
53415379

53425380

5381+
@pytest.mark.parametrize('err, args, kwargs, match',
5382+
((ValueError, (-1,), {},
5383+
'margin must be greater than -0.5'),
5384+
(ValueError, (1, -1), {},
5385+
'margin must be greater than -0.5'),
5386+
(ValueError, tuple(), {'x': -1},
5387+
'margin must be greater than -0.5'),
5388+
(ValueError, tuple(), {'y': -1},
5389+
'margin must be greater than -0.5'),
5390+
(TypeError, (1, ), {'x': 1, 'y': 1},
5391+
'Cannot pass both positional and keyword '
5392+
'arguments for x and/or y.'),
5393+
(TypeError, (1, 1, 1), {},
5394+
'Must pass a single positional argument for all*'),
5395+
))
5396+
def test_margins_errors(err, args, kwargs, match):
5397+
with pytest.raises(err, match=match):
5398+
fig = plt.figure()
5399+
ax = fig.add_subplot()
5400+
ax.margins(*args, **kwargs)
5401+
5402+
53435403
def test_length_one_hist():
53445404
fig, ax = plt.subplots()
53455405
ax.hist(1)
53465406
ax.hist([1])
53475407

53485408

5409+
def test_set_xy_bound():
5410+
fig = plt.figure()
5411+
ax = fig.add_subplot()
5412+
ax.set_xbound(2.0, 3.0)
5413+
assert ax.get_xbound() == (2.0, 3.0)
5414+
assert ax.get_xlim() == (2.0, 3.0)
5 13CC 415+
ax.set_xbound(upper=4.0)
5416+
assert ax.get_xbound() == (2.0, 4.0)
5417+
assert ax.get_xlim() == (2.0, 4.0)
5418+
ax.set_xbound(lower=3.0)
5419+
assert ax.get_xbound() == (3.0, 4.0)
5420+
assert ax.get_xlim() == (3.0, 4.0)
5421+
5422+
ax.set_ybound(2.0, 3.0)
5423+
assert ax.get_ybound() == (2.0, 3.0)
5424+
assert ax.get_ylim() == (2.0, 3.0)
5425+
ax.set_ybound(upper=4.0)
5426+
assert ax.get_ybound() == (2.0, 4.0)
5427+
assert ax.get_ylim() == (2.0, 4.0)
5428+
ax.set_ybound(lower=3.0)
5429+
assert ax.get_ybound() == (3.0, 4.0)
5430+
assert ax.get_ylim() == (3.0, 4.0)
5431+
5432+
53495433
def test_pathological_hexbin():
53505434
# issue #2863
53515435
mylist = [10] * 100
@@ -6018,6 +6102,7 @@ def test_axisbelow():
60186102
left=False, right=False)
60196103
ax.spines[:].set_visible(False)
60206104
ax.set_axisbelow(setting)
6105+
assert ax.get_axisbelow() == setting
60216106

60226107

60236108
def test_titletwiny():
@@ -6534,6 +6619,12 @@ def test_secondary_formatter():
65346619
secax.xaxis.get_major_formatter(), mticker.ScalarFormatter)
65356620

65366621

6622+
def test_secondary_repr():
6623+
fig, ax = plt.subplots()
6624+
secax = ax.secondary_xaxis("top")
6625+
assert repr(secax) == '<SecondaryAxis:>'
6626+
6627+
65376628
def color_boxes(fig, ax):
65386629
"""
65396630
Helper for the tests below that test the extents of various axes elements
@@ -6757,6 +6848,23 @@ def test_axis_extent_arg():
67576848
assert (ymin, ymax) == ax.get_ylim()
67586849

67596850

6851+
def test_axis_extent_arg2():
6852+
# Same as test_axis_extent_arg, but with keyword arguments
6853+
fig, ax = plt.subplots()
6854+
xmin = 5
6855+
xmax = 10
6856+
ymin = 15
6857+
ymax = 20
6858+
extent = ax.axis(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax)
6859+
6860+
# test that the docstring is correct
6861+
assert tuple(extent) == (xmin, xmax, ymin, ymax)
6862+
6863+
# test that limits were set per the docstring
6864+
assert (xmin, xmax) == ax.get_xlim()
6865+
assert (ymin, ymax) == ax.get_ylim()
6866+
6867+
67606868
def test_datetime_masked():
67616869
# make sure that all-masked data falls back to the viewlim
67626870
# set in convert.axisinfo....

0 commit comments

Comments
 (0)
0