8000 Merge pull request #22325 from oscargus/auto-backport-of-pr-22283-on-… · matplotlib/matplotlib@ffb11cd · GitHub
[go: up one dir, main page]

Skip to content

Commit ffb11cd

Browse files
authored
Merge pull request #22325 from oscargus/auto-backport-of-pr-22283-on-v3.5.x
Backport PR #22283: Fixed `repr` for `SecondaryAxis`
2 parents 9040870 + 44a4c52 commit ffb11cd

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
@@ -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: 101 additions & 0 deletions
81E4
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():
@@ -4949,6 +4963,23 @@ def test_shared_with_aspect_3():
49494963
assert round(expected, 4) == round(ax.get_aspect(), 4)
49504964

49514965

4966+
@pytest.mark.parametrize('err, args, kwargs, match',
4967+
((TypeError, (1, 2), {},
4968+
r"axis\(\) takes 0 or 1 positional arguments but 2"
4969+
" were given"),
4970+
(ValueError, ('foo', ), {},
4971+
"Unrecognized string foo to axis; try on or off"),
4972+
(TypeError, ([1, 2], ), {},
4973+
"the first argument to axis*"),
4974+
(TypeError, tuple(), {'foo': None},
4975+
r"axis\(\) got an unexpected keyword argument "
4976+
"'foo'"),
4977+
))
4978+
def test_axis_errors(err, args, kwargs, match):
4979+
with pytest.raises(err, match=match):
4980+
plt.axis(*args, **kwargs)
4981+
4982+
49524983
@pytest.mark.parametrize('twin', ('x', 'y'))
49534984
def test_twin_with_aspect(twin):
49544985
fig, ax = plt.subplots()
@@ -5342,12 +5373,58 @@ def test_set_margin_updates_limits():
53425373
assert ax.get_xlim() == (1, 2)
53435374

53445375

5376+
@pytest.mark.parametrize('err, args, kwargs, match',
5377+
((ValueError, (-1,), {},
5378+
'margin must be greater than -0.5'),
5379+
(ValueError, (1, -1), {},
5380+
'margin must be greater than -0.5'),
5381+
(ValueError, tuple(), {'x': -1},
5382+
'margin must be greater than -0.5'),
5383+
(ValueError, tuple(), {'y': -1},
5384+
'margin must be greater than -0.5'),
5385+
(TypeError, (1, ), {'x': 1, 'y': 1},
5386+
'Cannot pass both positional and keyword '
5387+
'arguments for x and/or y.'),
5388+
(TypeError, (1, 1, 1), {},
5389+
'Must pass a single positional argument for all*'),
5390+
))
5391+
def test_margins_errors(err, args, kwargs, match):
5392+
with pytest.raises(err, match=match):
5393+
fig = plt.figure()
5394+
ax = fig.add_subplot()
5395+
ax.margins(*args, **kwargs)
5396+
5397+
53455398
def test_length_one_hist():
53465399
fig, ax = plt.subplots()
53475400
ax.hist(1)
53485401
ax.hist([1])
53495402

53505403

5404+
def test_set_xy_bound():
5405+
fig = plt.figure()
5406+
ax = fig.add_subplot()
5407+
ax.set_xbound(2.0, 3.0)
5408+
assert ax.get_xbound() == (2.0, 3.0)
5409+
assert ax.get_xlim() == (2.0, 3.0)
5410+
ax.set_xbound(upper=4.0)
5411+
assert ax.get_xbound() == (2.0, 4.0)
5412+
assert ax.get_xlim() == (2.0, 4.0)
5413+
ax.set_xbound(lower=3.0)
5414+
assert ax.get_xbound() == (3.0, 4.0)
5415+
assert ax.get_xlim() == (3.0, 4.0)
5416+
5417+
ax.set_ybound(2.0, 3.0)
5418+
assert ax.get_ybound() == (2.0, 3.0)
5419+
assert ax.get_ylim() == (2.0, 3.0)
5420+
ax.set_ybound(upper=4.0)
5421+
assert ax.get_ybound() == (2.0, 4.0)
5422+
assert ax.get_ylim() == (2.0, 4.0)
5423+
ax.set_ybound(lower=3.0)
5424+
assert ax.get_ybound() == (3.0, 4.0)
5425+
assert ax.get_ylim() == (3.0, 4.0)
5426+
5427+
53515428
def test_pathological_hexbin():
53525429
# issue #2863
53535430
mylist = [10] * 100
@@ -6020,6 +6097,7 @@ def test_axisbelow():
60206097
left=False, right=False)
60216098
ax.spines[:].set_visible(False)
60226099
ax.set_axisbelow(setting)
6100+
assert ax.get_axisbelow() == setting
60236101

60246102

60256103
def test_titletwiny():
@@ -6536,6 +6614,12 @@ def test_secondary_formatter():
65366614
secax.xaxis.get_major_formatter(), mticker.ScalarFormatter)
65376615

65386616

6617+
def test_secondary_repr():
6618+
fig, ax = plt.subplots()
6619+
secax = ax.secondary_xaxis("top")
6620+
assert repr(secax) == '<SecondaryAxis:>'
6621+
6622+
65396623
def color_boxes(fig, ax):
65406624
"""
65416625
Helper for the tests below that test the extents of various axes elements
@@ -6759,6 +6843,23 @@ def test_axis_extent_arg():
67596843
assert (ymin, ymax) == ax.get_ylim()
67606844

67616845

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

0 commit comments

Comments
 (0)
0