8000 Fixed repr for SecondaryAxis · matplotlib/matplotlib@f6e1166 · GitHub
[go: up one dir, main page]

Skip to content

Commit f6e1166

Browse files
committed
Fixed repr for SecondaryAxis
and increase coverage for axes/_base.py
1 parent f393802 commit f6e1166

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +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)
711+
if hasattr(self, 'get_title'):
712+
title = self.get_title(loc=k)
713+
else:
714+
title = ""
712715
if title:
713716
titles.append(f"{k!r}:{title!r}")
714717
if titles:

lib/matplotlib/tests/test_axes.py

Lines changed: 100 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() is True
494+
assert ax.get_autoscaley_on() is True
495+
assert ax.get_autoscale_on() is True
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() is True
503+
assert ax.get_autoscaley_on() is True
504+
assert ax.get_autoscale_on() is True
505+
492506

493507
@mpl.style.context('default')
494508
def test_autoscale_log_shared():
@@ -4982,6 +4996,22 @@ 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+
def test_axis_errors(err, args, kwargs, match):
5011+
with pytest.raises(err, match=match):
5012+
plt.axis(*args, **kwargs)
5013+
5014+
49855015
@pytest.mark.parametrize('twin', ('x', 'y'))
49865016
def test_twin_with_aspect(twin):
49875017
fig, ax = plt.subplots()
@@ -5375,12 +5405,58 @@ def test_set_margin_updates_limits():
53755405
assert ax.get_xlim() == (1, 2)
53765406

53775407

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

53835435

5436+
def test_set_xy_bound():
5437+
fig = plt.figure()
5438+
ax = fig.add_subplot()
5439+
ax.set_xbound(2.0, 3.0)
5440+
assert ax.get_xbound() == (2.0, 3.0)
5441+
assert ax.get_xlim() == (2.0, 3.0)
5442+
ax.set_xbound(upper=4.0)
5443+
assert ax.get_xbound() == (2.0, 4.0)
5444+
assert ax.get_xlim() == (2.0, 4.0)
5445+
ax.set_xbound(lower=3.0)
5446+
assert ax.get_xbound() == (3.0, 4.0)
5447+
assert ax.get_xlim() == (3.0, 4.0)
5448+
5449+
ax.set_ybound(2.0, 3.0)
5450+
assert ax.get_ybound() == (2.0, 3.0)
5451+
assert ax.get_ylim() == (2.0, 3.0)
5452+
ax.set_ybound(upper=4.0)
5453+
assert ax.get_ybound() == (2.0, 4.0)
5454+
assert ax.get_ylim() == (2.0, 4.0)
5455+
ax.set_ybound(lower=3.0)
5456+
assert ax.get_ybound() == (3.0, 4.0)
5457+
assert ax.get_ylim() == (3.0, 4.0)
5458+
5459+
53845460
def test_pathological_hexbin():
53855461
# issue #2863
53865462
mylist = [10] * 100
@@ -6067,6 +6143,7 @@ def test_axisbelow():
60676143
left=False, right=False)
60686144
ax.spines[:].set_visible(False)
60696145
ax.set_axisbelow(setting)
6146+
assert ax.get_axisbelow() == setting
60706147

60716148

60726149
def test_titletwiny():
@@ -6613,6 +6690,12 @@ def test_secondary_formatter():
66136690
secax.xaxis.get_major_formatter(), mticker.ScalarFormatter)
66146691

66156692

6693+
def test_secondary_repr():
6694+
fig, ax = plt.subplots()
6695+
secax = ax.secondary_xaxis("top")
6696+
assert repr(secax) == '<SecondaryAxis:>'
6697+
6698+
66166699
def color_boxes(fig, ax):
66176700
"""
66186701
Helper for the tests below that test the extents of various axes elements
@@ -6836,6 +6919,23 @@ def test_axis_extent_arg():
68366919
assert (ymin, ymax) == ax.get_ylim()
68376920

68386921

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

0 commit comments

Comments
 (0)
0