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

Skip to content

Commit fe001ef

Browse files
authored
Merge pull request #10800 from matplotlib/auto-backport-of-pr-10787
Backport PR #10787 on branch v2.2.x
2 parents 3dd8f18 + 687bb95 commit fe001ef

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

lib/matplotlib/offsetbox.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,16 @@ def _get_packed_offsets(wd_list, total, sep, mode="fixed"):
7777
return total, offsets
7878

7979
elif mode == "expand":
80+
# This is a bit of a hack to avoid a TypeError when *total*
81+
# is None and used in conjugation with tight layout.
82+
if total is None:
83+
total = 1
8084
if len(w_list) > 1:
81-
sep = (total - sum(w_list)) / (len(w_list) - 1.)
85+
sep = (total - sum(w_list)) / (len(w_list) - 1)
8286
else:
8387
sep = 0
8488
offsets_ = np.cumsum([0] + [w + sep for w in w_list])
8589
offsets = offsets_[:-1]
86-
# this is a bit of a hack to avoid a TypeError when used
87-
# in conjugation with tight layout
88-
if total is None:
89-
total = 1
9090
return total, offsets
9191

9292
elif mode == "equal":

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from __future__ import absolute_import, division, print_function
22

3+
import pytest
34
from matplotlib.testing.decorators import image_comparison
45
import matplotlib.pyplot as plt
56
import matplotlib.patches as mpatches
67
import matplotlib.lines as mlines
7-
from matplotlib.offsetbox import AnchoredOffsetbox, DrawingArea
8+
from matplotlib.offsetbox import (
9+
AnchoredOffsetbox, DrawingArea, _get_packed_offsets)
810

911

1012
@image_comparison(baseline_images=['offsetbox_clipping'], remove_text=True)
@@ -101,16 +103,26 @@ def test_offsetbox_loc_codes():
101103

102104

103105
def test_expand_with_tight_layout():
104-
fig = plt.figure()
105-
axes = fig.add_subplot(111)
106-
107-
d1 = [29388871, 12448, 40, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
108-
0, 0, 0]
109-
d2 = [28396236, 981940, 22171, 537, 123, 88, 41, 42, 40, 26, 26,
110-
84, 6, 2, 0, 0, 0, 0, 0]
111-
axes.plot(d1, label='series 1')
112-
axes.plot(d2, label='series 2')
113-
axes.legend(mode='expand')
114-
115-
# ### THIS IS WHERE THE CRASH HAPPENS
116-
plt.tight_layout(rect=[0, 0.08, 1, 0.92])
106+
# Check issue reported in #10476, and updated due to #10784
107+
fig, ax = plt.subplots()
108+
109+
d1 = [1, 2]
110+
d2 = [2, 1]
111+
ax.plot(d1, label='series 1')
112+
ax.plot(d2, label='series 2')
113+
ax.legend(ncol=2, mode='expand')
114+
115+
fig.tight_layout() # where the crash used to happen
116+
117+
118+
@pytest.mark.parametrize('wd_list',
119+
([(150, 1)], [(150, 1)]*3, [(0.1, 1)], [(0.1, 1)]*2))
120+
@pytest.mark.parametrize('total', (250, 100, 0, -1, None))
121+
@pytest.mark.parametrize('sep', (250, 1, 0, -1))
122+
@pytest.mark.parametrize('mode', ("expand", "fixed", "equal"))
123+
def test_get_packed_offsets(wd_list, total, sep, mode):
124+
# Check a (rather arbitrary) set of parameters due to successive similar
125+
# issue tickets (at least #10476 and #10784) related to corner cases
126+
# triggered inside this function when calling higher-level functions
127+
# (e.g. `Axes.legend`).
128+
_get_packed_offsets(wd_list, total, sep, mode=mode)

0 commit comments

Comments
 (0)