Skip to content

Commit 075067c

Browse files
authored
Merge pull request matplotlib#22283 from oscargus/axestests
Fixed `repr` for `SecondaryAxis`
2 parents ba22d49 + d710ab2 commit 075067c

File tree

2 files changed

+105
-3
lines changed

2 files changed

+105
-3
lines changed

lib/matplotlib/axes/_base.py

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

lib/matplotlib/tests/test_axes.py

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

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

493507
@mpl.style.context('default')
494508
def test_autoscale_log_shared():
@@ -4982,6 +4996,23 @@ def test_shared_aspect_error():
49824996
fig.draw_without_rendering()
49834997

49844998

4999+
@pytest.mark.parametrize('err, args, kwargs, match',
5000+
((TypeError, (1, 2), {},
5001+
r"axis\(\) takes 0 or 1 positional arguments but 2"
5002+
" were given"),
5003+
(ValueError, ('foo', ), {},
5004+
"Unrecognized string foo to axis; try on or off"),
5005+
(TypeError, ([1, 2], ), {},
5006+
"the first argument to axis*"),
5007+
(TypeError, tuple(), {'foo': None},
5008+
r"axis\(\) got an unexpected keyword argument "
5009+
"'foo'"),
5010+
))
5011+
def test_axis_errors(err, args, kwargs, match):
5012+
with pytest.raises(err, match=match):
5013+
plt.axis(*args, **kwargs)
5014+
5015+
49855016
@pytest.mark.parametrize('twin', ('x', 'y'))
49865017
def test_twin_with_aspect(twin):
49875018
fig, ax = plt.subplots()
@@ -5375,12 +5406,58 @@ def test_set_margin_updates_limits():
53755406
assert ax.get_xlim() == (1, 2)
53765407

53775408

5409+
@pytest.mark.parametrize('err, args, kwargs, match',
5410+
((ValueError, (-1,), {},
5411+
'margin must be greater than -0.5'),
5412+
(ValueError, (1, -1), {},
5413+
'margin must be greater than -0.5'),
5414+
(ValueError, tuple(), {'x': -1},
5415+
'margin must be greater than -0.5'),
5416+
(ValueError, tuple(), {'y': -1},
5417+
'margin must be greater than -0.5'),
5418+
(TypeError, (1, ), {'x': 1, 'y': 1},
5419+
'Cannot pass both positional and keyword '
5420+
'arguments for x and/or y.'),
5421+
(TypeError, (1, 1, 1), {},
5422+
'Must pass a single positional argument for all*'),
5423+
))
5424+
def test_margins_errors(err, args, kwargs, match):
5425+
with pytest.raises(err, match=match):
5426+
fig = plt.figure()
5427+
ax = fig.add_subplot()
5428+
ax.margins(*args, **kwargs)
5429+
5430+
53785431
def test_length_one_hist():
53795432
fig, ax = plt.subplots()
53805433
ax.hist(1)
53815434
ax.hist([1])
53825435

53835436

5437+
def test_set_xy_bound():
5438+
fig = plt.figure()
5439+
ax = fig.add_subplot()
5440+
ax.set_xbound(2.0, 3.0)
5441+
assert ax.get_xbound() == (2.0, 3.0)
5442+
assert ax.get_xlim() == (2.0, 3.0)
5443+
ax.set_xbound(upper=4.0)
5444+
assert ax.get_xbound() == (2.0, 4.0)
5445+
assert ax.get_xlim() == (2.0, 4.0)
5446+
ax.set_xbound(lower=3.0)
5447+
assert ax.get_xbound() == (3.0, 4.0)
5448+
assert ax.get_xlim() == (3.0, 4.0)
5449+
5450+
ax.set_ybound(2.0, 3.0)
5451+
assert ax.get_ybound() == (2.0, 3.0)
5452+
assert ax.get_ylim() == (2.0, 3.0)
5453+
ax.set_ybound(upper=4.0)
5454+
assert ax.get_ybound() == (2.0, 4.0)
5455+
assert ax.get_ylim() == (2.0, 4.0)
5456+
ax.set_ybound(lower=3.0)
5457+
assert ax.get_ybound() == (3.0, 4.0)
5458+
assert ax.get_ylim() == (3.0, 4.0)
5459+
5460+
53845461
def test_pathological_hexbin():
53855462
# issue #2863
53865463
mylist = [10] * 100
@@ -6067,6 +6144,7 @@ def test_axisbelow():
60676144
left=False, right=False)
60686145
ax.spines[:].set_visible(False)
60696146
ax.set_axisbelow(setting)
6147+
assert ax.get_axisbelow() == setting
60706148

60716149

60726150
def test_titletwiny():
@@ -6613,6 +6691,12 @@ def test_secondary_formatter():
66136691
secax.xaxis.get_major_formatter(), mticker.ScalarFormatter)
66146692

66156693

6694+
def test_secondary_repr():
6695+
fig, ax = plt.subplots()
6696+
secax = ax.secondary_xaxis("top")
6697+
assert repr(secax) == '<SecondaryAxis:>'
6698+
6699+
66166700
def color_boxes(fig, ax):
66176701
"""
66186702
Helper for the tests below that test the extents of various axes elements
@@ -6836,6 +6920,23 @@ def test_axis_extent_arg():
68366920
assert (ymin, ymax) == ax.get_ylim()
68376921

68386922

6923+
def test_axis_extent_arg2():
6924+
# Same as test_axis_extent_arg, but with keyword arguments
6925+
fig, ax = plt.subplots()
6926+
xmin = 5
6927+
xmax = 10
6928+
ymin = 15
6929+
ymax = 20
6930+
extent = ax.axis(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax)
6931+
6932+
# test that the docstring is correct
6933+
assert tuple(extent) == (xmin, xmax, ymin, ymax)
6934+
6935+
# test that limits were set per the docstring
6936+
assert (xmin, xmax) == ax.get_xlim()
6937+
assert (ymin, ymax) == ax.get_ylim()
6938+
6939+
68396940
def test_datetime_masked():
68406941
# make sure that all-masked data falls back to the viewlim
68416942
# set in convert.axisinfo....

0 commit comments

Comments
 (0)