diff --git a/lib/matplotlib/offsetbox.py b/lib/matplotlib/offsetbox.py index 8ac5f7a1b5ad..6f4f1ebc4ffd 100644 --- a/lib/matplotlib/offsetbox.py +++ b/lib/matplotlib/offsetbox.py @@ -73,7 +73,10 @@ def _get_packed_offsets(wd_list, total, sep, mode="fixed"): return total, offsets elif mode == "expand": - sep = (total - sum(w_list)) / (len(w_list) - 1.) + if len(w_list) > 1: + sep = (total - sum(w_list)) / (len(w_list) - 1.) + else: + sep = 0. offsets_ = np.add.accumulate([0] + [w + sep for w in w_list]) offsets = offsets_[:-1] diff --git a/lib/matplotlib/tests/baseline_images/test_legend/legend_expand.pdf b/lib/matplotlib/tests/baseline_images/test_legend/legend_expand.pdf new file mode 100644 index 000000000000..ccae6aab282f Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_legend/legend_expand.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_legend/legend_expand.png b/lib/matplotlib/tests/baseline_images/test_legend/legend_expand.png new file mode 100644 index 000000000000..0c03b851fa23 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_legend/legend_expand.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_legend/legend_expand.svg b/lib/matplotlib/tests/baseline_images/test_legend/legend_expand.svg new file mode 100644 index 000000000000..da406bcfdfae --- /dev/null +++ b/lib/matplotlib/tests/baseline_images/test_legend/legend_expand.svg @@ -0,0 +1,1157 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index f192fb4b23af..21c3ac5e85e6 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -46,3 +46,19 @@ def test_fancy(): plt.errorbar(range(10), range(10), xerr=0.5, yerr=0.5, label='XX') plt.legend(loc="center left", bbox_to_anchor=[1.0, 0.5], ncol=2, shadow=True, title="My legend", numpoints=1) + + +@image_comparison(baseline_images=['legend_expand'], remove_text=True) +def test_legend_expand(): + 'Test expand mode' + legend_modes = [None, "expand"] + fig, axes_list = plt.subplots(len(legend_modes), 1) + x = np.arange(100) + for ax, mode in zip(axes_list, legend_modes): + ax.plot(x, 50 - x, 'o', label='y=1') + l1 = ax.legend(loc=2, mode=mode) + ax.add_artist(l1) + ax.plot(x, x - 50, 'o', label='y=-1') + l2 = ax.legend(loc=5, mode=mode) + ax.add_artist(l2) + ax.legend(loc=3, mode=mode, ncol=2)