8000 ENH: add vertical layout · matplotlib/matplotlib@8e21c15 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e21c15

Browse files
committed
ENH: add vertical layout
1 parent 2ebe797 commit 8e21c15

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

lib/matplotlib/figure.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,11 +1726,14 @@ def legend(self, *args, outside=False, axs=None, **kwargs):
17261726
Parameters
17271727
----------
17281728
1729-
outside: bool
1729+
outside: bool or string
17301730
If ``constrained_layout=True``, then try and place legend outside
17311731
axes listed in *axs*, or highest-level gridspec if axs is empty.
17321732
Note, "center" and "best" options to *loc* do not work with
1733-
``outside=True``
1733+
``outside=True``. The corner values of *loc* (i.e. "upper right")
1734+
will default to a horizontal layout of the legend, but this can
1735+
be changed by specifying a string
1736+
``outside="vertical", loc="upper right"``.
17341737
17351738
axs : sequence of `~.axes.Axes`
17361739
axes to gather handles from (if *handles* is empty).
@@ -1791,8 +1794,8 @@ def legend(self, *args, outside=False, axs=None, **kwargs):
17911794
gs = axs
17921795
else:
17931796
gs = axs[0].get_gridspec()
1794-
l = gs.legend_outside(loc=loc, handles=handles, labels=labels,
1795-
**kwargs)
1797+
l = gs.legend_outside(loc=loc, align=outside, handles=handles,
1798+
labels=labels, **kwargs)
17961799
l._remove_method = self.legends.remove
17971800
self.stale = True
17981801
return l

lib/matplotlib/gridspec.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ def _normalize(key, size): # Includes last index.
172172

173173
return SubplotSpec(self, num1, num2)
174174

175-
def legend_outside(self, handles=None, labels=None, axs=None, **kwargs):
175+
def legend_outside(self, handles=None, labels=None, axs=None,
176+
align='horizontal', **kwargs):
176177
"""
177178
legend for this gridspec, offset from all the subplots.
178179
@@ -201,18 +202,32 @@ def legend_outside(self, handles=None, labels=None, axs=None, **kwargs):
201202

202203
leg._update_width_height()
203204

205+
if leg._loc in [5, 7, 4, 1]:
206+
stack = 'right'
207+
elif leg._loc in [6, 2, 3]:
208+
stack = 'left'
209+
elif leg._loc in [8]:
210+
stack = 'bottom'
211+
else:
212+
stack = 'top'
213+
214+
if align == 'vertical':
215+
if leg._loc in [1, 2]:
216+
stack = 'top'
217+
elif leg._loc in [3, 4]:
218+
stack = 'bottom'
219+
204220
for child in self._layoutbox.children:
205221
if child._is_subplotspec_layoutbox():
206-
if leg._loc in [1, 4, 5, 7]:
207-
# stack to the right...
222+
if stack == 'right':
208223
layoutbox.hstack([child, leg._layoutbox], padding=paddingw)
209-
elif leg._loc in [2, 3, 6]:
224+
elif stack == 'left':
210225
# stack to the left...
211226
layoutbox.hstack([leg._layoutbox, child], padding=paddingw)
212-
elif leg._loc in [8]:
227+
elif stack == 'bottom':
213228
# stack to the bottom...
214229
layoutbox.vstack([child, leg._layoutbox], padding=paddingh)
215-
elif leg._loc in [9]:
230+
elif stack == 'top':
216231
# stack to the top...
217232
layoutbox.vstack([leg._layoutbox, child], padding=paddingh)
218233
self.figure.legends.append(leg)

0 commit comments

Comments
 (0)
0