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

Skip to content

Commit 218cdff

Browse files
authored
Merge pull request #24462 from anntzer/gpo
Don't pass unused xdescent to _get_packed_offsets.
2 parents 90617dd + 8ef4e01 commit 218cdff

File tree

2 files changed

+37
-44
lines changed

2 files changed

+37
-44
lines changed

lib/matplotlib/offsetbox.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,13 @@ def bbox_artist(*args, **kwargs):
4646
mbbox_artist(*args, **kwargs)
4747

4848

49-
def _get_packed_offsets(wd_list, total, sep, mode="fixed"):
49+
def _get_packed_offsets(widths, total, sep, mode="fixed"):
5050
r"""
51-
Pack boxes specified by their ``(width, xdescent)`` pair.
51+
Pack boxes specified by their *widths*.
5252
5353
For simplicity of the description, the terminology used here assumes a
5454
horizontal layout, but the function works equally for a vertical layout.
5555
56-
*xdescent* is analogous to the usual descent, but along the x-direction; it
57-
is currently ignored.
58-
5956
There are three packing *mode*\s:
6057
6158
- 'fixed': The elements are packed tight to the left with a spacing of
@@ -79,8 +76,8 @@ def _get_packed_offsets(wd_list, total, sep, mode="fixed"):
7976
8077
Parameters
8178
----------
82-
wd_list : list of (float, float)
83-
(width, xdescent) of boxes to be packed.
79+
widths : list of float
80+
Widths of boxes to be packed.
8481
total : float or None
8582
Intended total length. *None* if not used.
8683
sep : float
@@ -95,11 +92,10 @@ def _get_packed_offsets(wd_list, total, sep, mode="fixed"):
9592
offsets : array of float
9693
The left offsets of the boxes.
9794
"""
98-
w_list, d_list = zip(*wd_list) # d_list is currently not used.
9995
_api.check_in_list(["fixed", "expand", "equal"], mode=mode)
10096

10197
if mode == "fixed":
102-
offsets_ = np.cumsum([0] + [w + sep for w in w_list])
98+
offsets_ = np.cumsum([0] + [w + sep for w in widths])
10399
offsets = offsets_[:-1]
104100
if total is None:
105101
total = offsets_[-1] - sep
@@ -110,24 +106,24 @@ def _get_packed_offsets(wd_list, total, sep, mode="fixed"):
110106
# is None and used in conjugation with tight layout.
111107
if total is None:
112108
total = 1
113-
if len(w_list) > 1:
114-
sep = (total - sum(w_list)) / (len(w_list) - 1)
109+
if len(widths) > 1:
110+
sep = (total - sum(widths)) / (len(widths) - 1)
115111
else:
116112
sep = 0
117-
offsets_ = np.cumsum([0] + [w + sep for w in w_list])
113+
offsets_ = np.cumsum([0] + [w + sep for w in widths])
118114
offsets = offsets_[:-1]
119115
return total, offsets
120116

121117
elif mode == "equal":
122-
maxh = max(w_list)
118+
maxh = max(widths)
123119
if total is None:
124120
if sep is None:
125121
raise ValueError("total and sep cannot both be None when "
126122
"using layout mode 'equal'")
127-
total = (maxh + sep) * len(w_list)
123+
total = (maxh + sep) * len(widths)
128124
else:
129-
sep = total / len(w_list) - maxh
130-
offsets = (maxh + sep) * np.arange(len(w_list))
125+
sep = total / len(widths) - maxh
126+
offsets = (maxh + sep) * np.arange(len(widths))
131127
return total, offsets
132128

133129

@@ -445,7 +441,7 @@ def get_extent_offsets(self, renderer):
445441
self.width,
446442
self.align)
447443

448-
pack_list = [(h, yd) for w, h, xd, yd in whd_list]
444+
pack_list = [h for w, h, xd, yd in whd_list]
449445
height, yoffsets_ = _get_packed_offsets(pack_list, self.height,
450446
sep, self.mode)
451447

@@ -483,8 +479,7 @@ def get_extent_offsets(self, renderer):
483479
self.height,
484480
self.align)
485481

486-
pack_list = [(w, xd) for w, h, xd, yd in whd_list]
487-
482+
pack_list = [w for w, h, xd, yd in whd_list]
488483
width, xoffsets_ = _get_packed_offsets(pack_list, self.width,
489484
sep, self.mode)
490485

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -122,71 +122,69 @@ def test_expand_with_tight_layout():
122122
fig.tight_layout() # where the crash used to happen
123123

124124

125-
@pytest.mark.parametrize('wd_list',
126-
([(150, 1)], [(150, 1)]*3, [(0.1, 1)], [(0.1, 1)]*2))
125+
@pytest.mark.parametrize('widths',
126+
([150], [150, 150, 150], [0.1], [0.1, 0.1]))
127127
@pytest.mark.parametrize('total', (250, 100, 0, -1, None))
128128
@pytest.mark.parametrize('sep', (250, 1, 0, -1))
129129
@pytest.mark.parametrize('mode', ("expand", "fixed", "equal"))
130-
def test_get_packed_offsets(wd_list, total, sep, mode):
130+
def test_get_packed_offsets(widths, total, sep, mode):
131131
# Check a (rather arbitrary) set of parameters due to successive similar
132132
# issue tickets (at least #10476 and #10784) related to corner cases
133133
# triggered inside this function when calling higher-level functions
134134
# (e.g. `Axes.legend`).
135135
# These are just some additional smoke tests. The output is untested.
136-
_get_packed_offsets(wd_list, total, sep, mode=mode)
136+
_get_packed_offsets(widths, total, sep, mode=mode)
137137

138138

139139
_Params = namedtuple('_params', 'wd_list, total, sep, expected')
140140

141141

142-
@pytest.mark.parametrize('wd_list, total, sep, expected', [
142+
@pytest.mark.parametrize('widths, total, sep, expected', [
143143
_Params( # total=None
144-
[(3, 0), (1, 0), (2, 0)], total=None, sep=1, expected=(8, [0, 4, 6])),
144+
[3, 1, 2], total=None, sep=1, expected=(8, [0, 4, 6])),
145145
_Params( # total larger than required
146-
[(3, 0), (1, 0), (2, 0)], total=10, sep=1, expected=(10, [0, 4, 6])),
146+
[3, 1, 2], total=10, sep=1, expected=(10, [0, 4, 6])),
147147
_Params( # total smaller than required
148-
[(3, 0), (1, 0), (2, 0)], total=5, sep=1, expected=(5, [0, 4, 6])),
148+
[3, 1, 2], total=5, sep=1, expected=(5, [0, 4, 6])),
149149
])
150-
def test_get_packed_offsets_fixed(wd_list, total, sep, expected):
151-
result = _get_packed_offsets(wd_list, total, sep, mode='fixed')
150+
def test_get_packed_offsets_fixed(widths, total, sep, expected):
151+
result = _get_packed_offsets(widths, total, sep, mode='fixed')
152152
assert result[0] == expected[0]
153153
assert_allclose(result[1], expected[1])
154154

155155

156-
@pytest.mark.parametrize('wd_list, total, sep, expected', [
156+
@pytest.mark.parametrize('widths, total, sep, expected', [
157157
_Params( # total=None (implicit 1)
158-
[(.1, 0)] * 3, total=None, sep=None, expected=(1, [0, .45, .9])),
158+
[.1, .1, .1], total=None, sep=None, expected=(1, [0, .45, .9])),
159159
_Params( # total larger than sum of widths
160-
[(3, 0), (1, 0), (2, 0)], total=10, sep=1, expected=(10, [0, 5, 8])),
160+
[3, 1, 2], total=10, sep=1, expected=(10, [0, 5, 8])),
161161
_Params( # total smaller sum of widths: overlapping boxes
162-
[(3, 0), (1, 0), (2, 0)], total=5, sep=1, expected=(5, [0, 2.5, 3])),
162+
[3, 1, 2], total=5, sep=1, expected=(5, [0, 2.5, 3])),
163163
])
164-
def test_get_packed_offsets_expand(wd_list, total, sep, expected):
165-
result = _get_packed_offsets(wd_list, total, sep, mode='expand')
164+
def test_get_packed_offsets_expand(widths, total, sep, expected):
165+
result = _get_packed_offsets(widths, total, sep, mode='expand')
166166
assert result[0] == expected[0]
167167
assert_allclose(result[1], expected[1])
168168

169169

170-
@pytest.mark.parametrize('wd_list, total, sep, expected', [
170+
@pytest.mark.parametrize('widths, total, sep, expected', [
171171
_Params( # total larger than required
172-
[(3, 0), (2, 0), (1, 0)], total=6, sep=None, expected=(6, [0, 2, 4])),
172+
[3, 2, 1], total=6, sep=None, expected=(6, [0, 2, 4])),
173173
_Params( # total smaller sum of widths: overlapping boxes
174-
[(3, 0), (2, 0), (1, 0), (.5, 0)], total=2, sep=None,
175-
expected=(2, [0, 0.5, 1, 1.5])),
174+
[3, 2, 1, .5], total=2, sep=None, expected=(2, [0, 0.5, 1, 1.5])),
176175
_Params( # total larger than required
177-
[(.5, 0), (1, 0), (.2, 0)], total=None, sep=1,
178-
expected=(6, [0, 2, 4])),
176+
[.5, 1, .2], total=None, sep=1, expected=(6, [0, 2, 4])),
179177
# the case total=None, sep=None is tested separately below
180178
])
181-
def test_get_packed_offsets_equal(wd_list, total, sep, expected):
182-
result = _get_packed_offsets(wd_list, total, sep, mode='equal')
179+
def test_get_packed_offsets_equal(widths, total, sep, expected):
180+
result = _get_packed_offsets(widths, total, sep, mode='equal')
183181
assert result[0] == expected[0]
184182
assert_allclose(result[1], expected[1])
185183

186184

187185
def test_get_packed_offsets_equal_total_none_sep_none():
188186
with pytest.raises(ValueError):
189-
_get_packed_offsets([(1, 0)] * 3, total=None, sep=None, mode='equal')
187+
_get_packed_offsets([1, 1, 1], total=None, sep=None, mode='equal')
190188

191189

192190
@pytest.mark.parametrize('child_type', ['draw', 'image', 'text'])

0 commit comments

Comments
 (0)