@@ -18,16 +18,13 @@ class (or others) to determine the size of each axes. The unit
18
18
class _Base :
19
19
20
20
def __rmul__ (self , other ):
21
- float (other ) # just to check if number if given
22
21
return Fraction (other , self )
23
22
24
23
def __add__ (self , other ):
25
24
if isinstance (other , _Base ):
26
25
return Add (self , other )
27
26
else :
28
- float (other )
29
- other = Fixed (other )
30
- return Add (self , other )
27
+ return Add (self , Fixed (other ))
31
28
32
29
33
30
class Add (_Base ):
@@ -55,7 +52,9 @@ class Fixed(_Base):
55
52
"""
56
53
Simple fixed size with absolute part = *fixed_size* and relative part = 0.
57
54
"""
55
+
58
56
def __init__ (self , fixed_size ):
57
+ cbook ._check_isinstance (Number , fixed_size = fixed_size )
59
58
self .fixed_size = fixed_size
60
59
61
60
def get_size (self , renderer ):
@@ -93,6 +92,7 @@ class AxesX(_Base):
93
92
Scaled size whose relative part corresponds to the data width
94
93
of the *axes* multiplied by the *aspect*.
95
94
"""
95
+
96
96
def __init__ (self , axes , aspect = 1. , ref_ax = None ):
97
97
self ._axes = axes
98
98
self ._aspect = aspect
@@ -118,6 +118,7 @@ class AxesY(_Base):
118
118
Scaled size whose relative part corresponds to the data height
119
119
of the *axes* multiplied by the *aspect*.
120
120
"""
121
+
121
122
def __init__ (self , axes , aspect = 1. , ref_ax = None ):
122
123
self ._axes = axes
123
124
self ._aspect = aspect
@@ -141,12 +142,12 @@ def get_size(self, renderer):
141
142
142
143
class MaxExtent (_Base ):
143
144
"""
144
- Size whose absolute part is the largest width ( or height) of
145
- the given *artist_list*.
145
+ Size whose absolute part is either the largest width or the largest height
146
+ of the given *artist_list*.
146
147
"""
148
+
147
149
def __init__ (self , artist_list , w_or_h ):
148
150
self ._artist_list = artist_list
149
-
150
151
cbook ._check_in_list (["width" , "height" ], w_or_h = w_or_h )
151
152
self ._w_or_h = w_or_h
152
153
@@ -155,74 +156,40 @@ def add_artist(self, a):
155
156
156
157
def get_size (self , renderer ):
157
158
rel_size = 0.
158
- w_list , h_list = [], []
159
- for a in self ._artist_list :
160
- bb = a .get_window_extent (renderer )
161
- w_list .append (bb .width )
162
- h_list .append (bb .height )
163
- dpi = a .get_figure ().get_dpi ()
164
- if self ._w_or_h == "width" :
165
- abs_size = max (w_list )/ dpi
166
- elif self ._w_or_h == "height" :
167
- abs_size = max (h_list )/ dpi
168
-
159
+ extent_list = [
160
+ getattr (a .get_window_extent (renderer ), self ._w_or_h ) / a .figure .dpi
161
+ for a in self ._artist_list ]
162
+ abs_size = max (extent_list , default = 0 )
169
163
return rel_size , abs_size
170
164
171
165
172
- class MaxWidth (_Base ):
166
+ class MaxWidth (MaxExtent ):
173
167
"""
174
- Size whose absolute part is the largest width of
175
- the given *artist_list*.
168
+ Size whose absolute part is the largest width of the given *artist_list*.
176
169
"""
177
- def __init__ (self , artist_list ):
178
- self ._artist_list = artist_list
179
-
180
- def add_artist (self , a ):
181
- self ._artist_list .append (a )
182
170
183
- def get_size (self , renderer ):
184
- rel_size = 0.
185
- w_list = []
186
- for a in self ._artist_list :
187
- bb = a .get_window_extent (renderer )
188
- w_list .append (bb .width )
189
- dpi = a .get_figure ().get_dpi ()
190
- abs_size = max (w_list )/ dpi
191
-
192
- return rel_size , abs_size
171
+ def __init__ (self , artist_list ):
172
+ super ().__init__ (artist_list , "width" )
193
173
194
174
195
- class MaxHeight (_Base ):
175
+ class MaxHeight (MaxExtent ):
196
176
"""
197
- Size whose absolute part is the largest height of
198
- the given *artist_list*.
177
+ Size whose absolute part is the largest height of the given *artist_list*.
199
178
"""
200
- def __init__ (self , artist_list ):
201
- self ._artist_list = artist_list
202
-
203
- def add_artist (self , a ):
204
- self ._artist_list .append (a )
205
-
206
- def get_size (self , renderer ):
207
- rel_size = 0.
208
- h_list = []
209
- for a in self ._artist_list :
210
- bb = a .get_window_extent (renderer )
211
- h_list .append (bb .height )
212
- dpi = a .get_figure ().get_dpi ()
213
- abs_size = max (h_list )/ dpi
214
179
215
- return rel_size , abs_size
180
+ def __init__ (self , artist_list ):
181
+ super ().__init__ (artist_list , "height" )
216
182
217
183
218
184
class Fraction (_Base ):
219
185
"""
220
186
An instance whose size is a *fraction* of the *ref_size*.
221
187
222
188
>>> s = Fraction(0.3, AxesX(ax))
223
-
224
189
"""
190
+
225
191
def __init__ (self , fraction , ref_size ):
192
+ cbook ._check_isinstance (Number , fraction = fraction )
226
193
self ._fraction_ref = ref_size
227
194
self ._fraction = fraction
228
195
@@ -241,6 +208,7 @@ class Padded(_Base):
241
208
Return a instance where the absolute part of *size* is
242
209
increase by the amount of *pad*.
243
210
"""
211
+
244
212
def __init__ (self , size , pad ):
245
213
self ._size = size
246
214
self ._pad = pad
@@ -256,18 +224,16 @@ def from_any(size, fraction_ref=None):
256
224
"""
257
225
Creates Fixed unit when the first argument is a float, or a
258
226
Fraction unit if that is a string that ends with %. The second
259
- argument is only meaningful when Fraction unit is created.::
260
-
261
- >>> a = Size.from_any(1.2) # => Size.Fixed(1.2)
262
- >>> Size.from_any("50%", a) # => Size.Fraction(0.5, a)
227
+ argument is only meaningful when Fraction unit is created.
263
228
229
+ >>> a = Size.from_any(1.2) # => Size.Fixed(1.2)
230
+ >>> Size.from_any("50%", a) # => Size.Fraction(0.5, a)
264
231
"""
265
232
if isinstance (size , Number ):
266
233
return Fixed (size )
267
234
elif isinstance (size , str ):
268
235
if size [- 1 ] == "%" :
269
236
return Fraction (float (size [:- 1 ]) / 100 , fraction_ref )
270
-
271
237
raise ValueError ("Unknown format" )
272
238
273
239
0 commit comments