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

Skip to content

Commit 44a4c52

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

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
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,23 @@ def test_shared_with_aspect_3():
49474961
assert round(expected, 4) == round(ax.get_aspect(), 4)
49484962

49494963

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

53425373

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

53485401

5402+
def test_set_xy_bound():
5403+
fig = plt.figure()
5404+
ax = fig.add_subplot()
5405+
ax.set_xbound(2.0, 3.0)
5406+
assert ax.get_xbound() == (2.0, 3.0)
5407+
assert ax.get_xlim() == (2.0, 3.0)
5408+
ax.set_xbound(upper=4.0)
5409+
assert ax.get_xbound() == (2.0, 4.0)
5410+
assert ax.get_xlim() == (2.0, 4.0)
5411+
ax.set_xbound(lower=3.0)
5412+
assert ax.get_xbound() == (3.0, 4.0)
5413+
assert ax.get_xlim() == (3.0, 4.0)
5414+
5415+
ax.set_ybound(2.0, 3.0)
5416+
assert ax.get_ybound() == (2.0, 3.0)
5417+
assert ax.get_ylim() == (2.0, 3.0)
5418+
ax.set_ybound(upper=4.0)
5419+
assert ax.get_ybound() == (2.0, 4.0)
5420+
assert ax.get_ylim() == (2.0, 4.0)
5421+
ax.set_ybound(lower=3.0)
5422+
assert ax.get_ybound() == (3.0, 4.0)
5423+
assert ax.get_ylim() == (3.0, 4.0)
5424+
5425+
53495426
def test_pathological_hexbin():
53505427
# issue #2863
53515428
mylist = [10] * 100
@@ -6018,6 +6095,7 @@ def test_axisbelow():
60186095
left=False, right=False)
60196096
ax.spines[:].set_visible(False)
60206097
ax.set_axisbelow(setting)
6098+
assert ax.get_axisbelow() == setting
60216099

60226100

60236101
def test_titletwiny():
@@ -6534,6 +6612,12 @@ def test_secondary_formatter():
65346612
secax.xaxis.get_major_formatter(), mticker.ScalarFormatter)
65356613

65366614

6615+
def test_secondary_repr():
6616+
fig, ax = plt.subplots()
6617+
secax = ax.secondary_xaxis("top")
6618+
assert repr(secax) == '<SecondaryAxis:>'
6619+
6620+
65376621
def color_boxes(fig, ax):
65386622
"""
65396623
Helper for the tests below that test the extents of various axes elements
@@ -6757,6 +6841,23 @@ def test_axis_extent_arg():
67576841
assert (ymin, ymax) == ax.get_ylim()
67586842

67596843

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

0 commit comments

Comments
 (0)
0