8000 Testing Added · matplotlib/matplotlib@ea2fa83 · GitHub
[go: up one dir, main page]

Skip to content

Commit ea2fa83

Browse files
committed
Testing Added
1 parent 74f1a80 commit ea2fa83

File tree

2 files changed

+185
-10
lines changed

2 files changed

+185
-10
lines changed

lib/matplotlib/figure.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,17 +2402,17 @@ def __repr__(self):
24022402
)
24032403

24042404
def __init__(self,
2405-
figsize=None, # not checked
2406-
dpi=None, #not checked
2405+
figsize=None,
2406+
dpi=None,
24072407
*,
2408-
facecolor=None, # not checked
2409-
edgecolor=None, # not checked
2410-
linewidth=0.0, #not checked
2411-
frameon=None, # not checked
2412-
subplotpars=None, # rc figure.subplot.* # getter setter
2413-
tight_layout=None, # rc figure.autolayout # not checked
2414-
constrained_layout=None, # rc figure.constrained_layout.use # not checked
2415-
layout=None, # not checked
2408+
facecolor=None,
2409+
edgecolor=None,
2410+
linewidth=0.0,
2411+
frameon=None,
2412+
subplotpars=None, # rc figure.subplot.*
2413+
tight_layout=None, # rc figure.autolayout
2414+
constrained_layout=None, # rc figure.constrained_layout.use
2415+
layout=None,
24162416
**kwargs
24172417
):
24182418
"""
@@ -2971,7 +2971,30 @@ def get_subplotpars(self):
29712971
matplotlib.figure.Figure.get_subplotpars
29722972
"""
29732973
return self.subplotpars
2974+
2975+
def set_figsize(self,fig_size_params):
2976+
self.set_size_inches(fig_size_params[0],fig_size_params[1])
2977+
"""
2978+
Calls all the set_size_inches() methods of the figure and its subfigures.
2979+
passes Parameters
2980+
"""
2981+
def get_figsize(self):
2982+
"""
2983+
Returns the size of the figure in inches
2984+
"""
2985+
return self.get_size_inches()
2986+
def set_layout(self, layout_params):
2987+
"""
2988+
Sets the layout of the figure.
2989+
"""
2990+
self.set_layout_engine(layout_params)
29742991

2992+
def get_layout(self):
2993+
"""
2994+
Returns the layout of the figure.
2995+
"""
2996+
return self.get_layout_engine()
2997+
29752998
@_docstring.interpd
29762999
def figimage(self, X, xo=0, yo=0, alpha=None, norm=None, cmap=None,
29773000
vmin=None, vmax=None, origin=None, resize=False, **kwargs):

lib/matplotlib/test_figure.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import pytest
2+
from PIL import Image
3+
4+
5+
import matplotlib as mpl
6+
from matplotlib import cbook, rcParams
7+
from matplotlib._api.deprecation import MatplotlibDeprecationWarning
8+
from matplotlib.testing.decorators import image_comparison, check_figures_equal
9+
from matplotlib.axes import Axes
10+
from matplotlib.figure import Figure, SubplotParams
11+
from matplotlib.ticker import AutoMinorLocator, FixedFormatter, ScalarFormatter
12+
import matplotlib.pyplot as plt
13+
import matplotlib.dates as mdates
14+
15+
fig.set_figsize(2, 4)
16+
assert fig.get_figsize()[0] == 2
17+
assert fig.get_figsize()[1] == 4
18+
fig = Figure(layout='tight')
19+
with pytest.warns(UserWarning, match="Figure parameters 'layout'=='tight' "
20+
"and 'tight_layout'==False cannot"):
21+
fig.set_layout(layout='tight', tight_layout=False)
22+
assert_is_tight(fig)
23+
24+
with pytest.warns(UserWarning, match="Figure parameters "
25+
"'layout'=='constrained' and "
26+
"'constrained_layout'==False cannot"):
27+
fig.set_layout(layout='constrained', constrained_layout=False)
28+
assert_is_constrained(fig)
29+
30+
with pytest.warns(UserWarning, match="Figure parameters "
31+
"'layout'=='tight' and "
32+
"'constrained_layout'!=False cannot"):
33+
fig.set_layout(layout='tight', constrained_layout=True)
34+
assert_is_tight(fig)
35+
36+
with pytest.warns(UserWarning, match="Figure parameters "
37+
"'layout'=='constrained' and "
38+
"'tight_layout'!=False cannot"):
39+
fig.set_layout(layout='constrained', tight_layout=True)
40+
assert_is_constrained(fig)
41+
42+
with pytest.warns(UserWarning, match="Figure parameters "
43+
"'layout'=='tight' and "
44+
"'constrained_layout'!=False cannot"):
45+
fig.set_layout(layout='tight', constrained_layout={'pad': 1})
46+
assert_is_tight(fig)
47+
with pytest.warns(UserWarning, match="Figure parameters "
48+
"'layout'=='constrained' and "
49+
"'tight_layout'!=False cannot"):
50+
fig.set_layout(layout='constrained', tight_layout={'pad': 1})
51+
assert_is_constrained(fig)
52+
53+
with pytest.warns(Warning) as warninfo:
54+
fig.set_layout(layout='tight',
55+
tight_layout=False,
56+
constrained_layout=True)
57+
warns = {(warn.category, warn.message.args[0]) for warn in warninfo}
58+
expected = {
59+
(UserWarning, "Figure parameters 'layout'=='tight' "
60+
"and 'tight_layout'==False cannot be used together. "
61+
"Please use 'layout' only."),
62+
(UserWarning, "Figure parameters 'layout'=='tight' "
63+
"and 'constrained_layout'!=False cannot be used together. "
64+
"Please use 'layout' only.")}
65+
assert_is_tight(fig)
66+
assert warns == expected
67+
with pytest.warns(Warning) as warninfo:
68+
fig.set_layout(layout='constrained',
69+
tight_layout=True,
70+
constrained_layout=False)
71+
warns = {(warn.category, warn.message.args[0]) for warn in warninfo}
72+
expected = {
73+
(UserWarning, "Figure parameters 'layout'=='constrained' "
74+
"and 'tight_layout'!=False cannot be used together. "
75+
"Please use 'layout' only."),
76+
(UserWarning, "Figure parameters 'layout'=='constrained' "
77+
"and 'constrained_layout'==False cannot be used together. "
78+
"Please use 'layout' only.")}
79+
assert_is_constrained(fig)
80+
assert warns == expected
81+
82+
with pytest.raises(ValueError,
83+
match="Cannot set 'tight_layout' and "
84+
"'constrained_layout' simultaneously."):
85+
fig = Figure(tight_layout={'w': 1}, constrained_layout={'w_pad': 1})
86+
with pytest.raises(ValueError,
87+
match="Cannot set 'tight_layout' and "
88+
"'constrained_layout' simultaneously."):
89+
fig = Figure(tight_layout=True, constrained_layout={'w_pad': 1})
90+
with pytest.raises(ValueError,
91+
match="Cannot set 'tight_layout' and "
92+
"'constrained_layout' simultaneously."):
93+
fig = Figure(tight_layout=True, constrained_layout=True)
94+
95+
96+
def test_set_subplotpars():
97+
subplotparams_keys = ["left", "bottom", "right", "top", "wspace", "hspace"]
98+
fig = plt.figure()
99+
subplotparams = fig.get_subplotpars()
100+
test_dict = {}
101+
default_dict = {}
102+
for key in subplotparams_keys:
103+
attr = getattr(subplotparams, key)
104+
assert attr == mpl.rcParams[f"figure.subplot.{key}"]
105+
default_dict[key] = attr
106+
test_dict[key] = attr * 2
107+
108+
subplotparams.update(left=test_dict['left'])
109+
assert fig.get_subplotpars().left == test_dict['left']
110+
111+
fig.subplots_adjust(**default_dict)
112+
assert fig.get_subplotpars().left == default_dict['left']
113+
114+
fig.set_subplotpars(test_dict)
115+
for key, value in test_dict.items():
116+
assert getattr(fig.get_subplotpars(), key) == value
117+
118+
test_subplotparams = SubplotParams()
119+
fig.set_subplotpars(test_subplotparams)
120+
for key, value in default_dict.items():
121+
assert getattr(fig.get_subplotpars(), key) == value
122+
123+
fig.set_subplotpars(test_dict)
124+
for key, value in test_dict.items():
125+
assert getattr(fig.get_subplotpars(), key) == value
126+
127+
test_dict['foo'] = 'bar'
128+
with pytest.warns(UserWarning,
129+
match="'foo' is not a valid key for set_subplotpars;"
130+
" this key was ignored"):
131+
fig.set_subplotpars(test_dict)
132+
133+
with pytest.raises(TypeError,
134+
match="subplotpars must be a dictionary of "
135+
"keyword-argument pairs or "
136+
"an instance of SubplotParams()"):
137+
fig.set_subplotpars(['foo'])
138+
139+
fig.set_subplotpars({})
140+
with pytest.raises(AttributeError): # test_dict['foo'] = 'bar'
141+
# but fig.get_subplotpars().foo should be invalid
142+
for key, value in test_dict.items():
143+
assert getattr(fig.get_subplotpars(), key) == value
144+
145+
def test_fig_get_set():
146+
varnames = filter(lambda var: var not in ['self', 'kwargs', 'args'],
147+
Figure.__init__.__code__.co_varnames)
148+
fig = plt.figure()
149+
for var in varnames:
150+
# if getattr fails then the getter and setter does not exist
151+
getfunc = getattr(fig, f"get_{var}")
152+
setfunc = getattr(fig, f"set_{var}")

0 commit comments

Comments
 (0)
0