Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a83134f

Browse files
committed
Add tests for new Figure layout argument.
1 parent e428983 commit a83134f

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

lib/matplotlib/figure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2241,7 +2241,7 @@ def __init__(self,
22412241
tight_layout = True
22422242
constrained_layout = False
22432243
else:
2244-
raise ValueError(f"Invalid value for 'layout': {layout!r}")
2244+
_api.check_in_list(['constrained', 'tight'], layout=layout)
22452245

22462246
self.callbacks = cbook.CallbackRegistry()
22472247
# Callbacks traditionally associated with the canvas (and exposed with

lib/matplotlib/tests/test_figure.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,13 +501,44 @@ def test_figure_repr():
501501
assert repr(fig) == "<Figure size 100x200 with 0 Axes>"
502502

503503

504-
def test_warn_cl_plus_tl():
504+
def test_valid_layouts():
505+
fig = Figure(layout=None)
506+
assert not fig.get_tight_layout()
507+
assert not fig.get_constrained_layout()
508+
509+
fig = Figure(layout='tight')
510+
assert fig.get_tight_layout()
511+
assert not fig.get_constrained_layout()
512+
513+
fig = Figure(layout='constrained')
514+
assert not fig.get_tight_layout()
515+
assert fig.get_constrained_layout()
516+
517+
518+
def test_invalid_layouts():
505519
fig, ax = plt.subplots(constrained_layout=True)
506520
with pytest.warns(UserWarning):
507521
# this should warn,
508522
fig.subplots_adjust(top=0.8)
509523
assert not(fig.get_constrained_layout())
510524

525+
# Using layout + (tight|constrained)_layout warns, but the former takes
526+
# precedence.
527+
with pytest.warns(UserWarning, match="Figure parameters 'layout' and "
528+
"'tight_layout' cannot"):
529+
fig = Figure(layout='tight', tight_layout=False)
530+
assert fig.get_tight_layout()
531+
assert not fig.get_constrained_layout()
532+
with pytest.warns(UserWarning, match="Figure parameters 'layout' and "
533+
"'constrained_layout' cannot"):
534+
fig = Figure(layout='constrained', constrained_layout=False)
535+
assert not fig.get_tight_layout()
536+
assert fig.get_constrained_layout()
537+
538+
with pytest.raises(ValueError,
539+
match="'foobar' is not a valid value for layout"):
540+
Figure(layout='foobar')
541+
511542

512543
@check_figures_equal(extensions=["png", "pdf"])
513544
def test_add_artist(fig_test, fig_ref):

0 commit comments

Comments
 (0)