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