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

Skip to content

Commit a118e75

Browse files
committed
Cleanup axes_size.
Rewrite MaxWidth, MaxHeight to reuse the MaxExtent logic; fix MaxExtent to work even if artist_list is empty. Docstrings cleanups. Move some typechecks (`float(...)` -> check_isinstance) to the class constructors (so that all calls benefit from it).
1 parent a57c9a1 commit a118e75

File tree

1 file changed

+26
-60
lines changed

1 file changed

+26
-60
lines changed

lib/mpl_toolkits/axes_grid1/axes_size.py

Lines changed: 26 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@ class (or others) to determine the size of each axes. The unit
1818
class _Base:
1919

2020
def __rmul__(self, other):
21-
float(other) # just to check if number if given
2221
return Fraction(other, self)
2322

2423
def __add__(self, other):
2524
if isinstance(other, _Base):
2625
return Add(self, other)
2726
else:
28-
float(other)
29-
other = Fixed(other)
30-
return Add(self, other)
27+
return Add(self, Fixed(other))
3128

3229

3330
class Add(_Base):
@@ -55,7 +52,9 @@ class Fixed(_Base):
5552
"""
5653
Simple fixed size with absolute part = *fixed_size* and relative part = 0.
5754
"""
55+
5856
def __init__(self, fixed_size):
57+
cbook._check_isinstance(Number, fixed_size=fixed_size)
5958
self.fixed_size = fixed_size
6059

6160
def get_size(self, renderer):
@@ -93,6 +92,7 @@ class AxesX(_Base):
9392
Scaled size whose relative part corresponds to the data width
9493
of the *axes* multiplied by the *aspect*.
9594
"""
95+
9696
def __init__(self, axes, aspect=1., ref_ax=None):
9797
self._axes = axes
9898
self._aspect = aspect
@@ -118,6 +118,7 @@ class AxesY(_Base):
118118
Scaled size whose relative part corresponds to the data height
119119
of the *axes* multiplied by the *aspect*.
120120
"""
121+
121122
def __init__(self, axes, aspect=1., ref_ax=None):
122123
self._axes = axes
123124
self._aspect = aspect
@@ -141,12 +142,12 @@ def get_size(self, renderer):
141142

142143
class MaxExtent(_Base):
143144
"""
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*.
146147
"""
148+
147149
def __init__(self, artist_list, w_or_h):
148150
self._artist_list = artist_list
149-
150151
cbook._check_in_list(["width", "height"], w_or_h=w_or_h)
151152
self._w_or_h = w_or_h
152153

@@ -155,74 +156,40 @@ def add_artist(self, a):
155156

156157
def get_size(self, renderer):
157158
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)
169163
return rel_size, abs_size
170164

171165

172-
class MaxWidth(_Base):
166+
class MaxWidth(MaxExtent):
173167
"""
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*.
176169
"""
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)
182170

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")
193173

194174

195-
class MaxHeight(_Base):
175+
class MaxHeight(MaxExtent):
196176
"""
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*.
199178
"""
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
214179

215-
return rel_size, abs_size
180+
def __init__(self, artist_list):
181+
super().__init__(artist_list, "height")
216182

217183

218184
class Fraction(_Base):
219185
"""
220186
An instance whose size is a *fraction* of the *ref_size*.
221187
222188
>>> s = Fraction(0.3, AxesX(ax))
223-
224189
"""
190+
225191
def __init__(self, fraction, ref_size):
192+
cbook._check_isinstance(Number, fraction=fraction)
226193
self._fraction_ref = ref_size
227194
self._fraction = fraction
228195

@@ -241,6 +208,7 @@ class Padded(_Base):
241208
Return a instance where the absolute part of *size* is
242209
increase by the amount of *pad*.
243210
"""
211+
244212
def __init__(self, size, pad):
245213
self._size = size
246214
self._pad = pad
@@ -256,18 +224,16 @@ def from_any(size, fraction_ref=None):
256224
"""
257225
Creates Fixed unit when the first argument is a float, or a
258226
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.
263228
229+
>>> a = Size.from_any(1.2) # => Size.Fixed(1.2)
230+
>>> Size.from_any("50%", a) # => Size.Fraction(0.5, a)
264231
"""
265232
if isinstance(size, Number):
266233
return Fixed(size)
267234
elif isinstance(size, str):
268235
if size[-1] == "%":
269236
return Fraction(float(size[:-1]) / 100, fraction_ref)
270-
271237
raise ValueError("Unknown format")
272238

273239

0 commit comments

Comments
 (0)