10000 ENH: add vertical layout · matplotlib/matplotlib@81209e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 81209e6

Browse files
committed
ENH: add vertical layout
1 parent 43ce090 commit 81209e6

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
@@ -1780,11 +1780,14 @@ def legend(self, *args, outside=False, axs=None, **kwargs):
17801780
Parameters
17811781
----------
17821782
1783-
outside: bool
1783+
outside: bool or string
17841784
If ``constrained_layout=True``, then try and place legend outside
17851785
axes listed in *axs*, or highest-level gridspec if axs is empty.
17861786
Note, "center" and "best" options to *loc* do not work with
1787-
``outside=True``
1787+
``outside=True``. The corner values of *loc* (i.e. "upper right")
1788+
will default to a horizontal layout of the legend, but this can
1789+
be changed by specifying a string
1790+
``outside="vertical", loc="upper right"``.
17881791
17891792
axs : sequence of `~.axes.Axes`
17901793
axes to gather handles from (if *handles* is empty).
@@ -1844,8 +1847,8 @@ def legend(self, *args, outside=False, axs=None, **kwargs):
18441847
gs = axs
18451848
else:
18461849
gs = axs[0].get_gridspec()
1847-
l = gs.legend_outside(loc=loc, handles=handles, labels=labels,
1848-
**kwargs)
1850+
l = gs.legend_outside(loc=loc, align=outside, handles=handles,
1851+
labels=labels, **kwargs)
18491852
l._remove_method = self.legends.remove
18501853
self.stale = True
18511854
return l
10000

lib/matplotlib/gridspec.py

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

177177
return SubplotSpec(self, num1, num2)
178178

179-
def legend_outside(self, handles=None, labels=None, axs=None, **kwargs):
179+
def legend_outside(self, handles=None, labels=None, axs=None,
180+
align='horizontal', **kwargs):
180181
"""
181182
legend for this gridspec, offset from all the subplots.
182183
@@ -205,18 +206,32 @@ def legend_outside(self, handles=None, labels=None, axs=None, **kwargs):
205206

206207
leg._update_width_height()
207208

209+
if leg._loc in [5, 7, 4, 1]:
210+
stack = 'right'
211+
elif leg._loc in [6, 2, 3]:
212+
stack = 'left'
213+
elif leg._loc in [8]:
214+
stack = 'bottom'
215+
else:
216+
stack = 'top'
217+
218+
if align == 'vertical':
219+
if leg._loc in [1, 2]:
220+
stack = 'top'
221+
elif leg._loc in [3, 4]:
222+
stack = 'bottom'
223+
208224
for child in self._layoutbox.children:
209225
if child._is_subplotspec_layoutbox():
210-
if leg._loc in [1, 4, 5, 7]:
211-
# stack to the right...
226+
if stack == 'right':
212227
layoutbox.hstack([child, leg._layoutbox], padding=paddingw)
213-
elif leg._loc in [2, 3, 6]:
228+
elif stack == 'left':
214229
# stack to the left...
215230
layoutbox.hstack([leg._layoutbox, child], padding=paddingw)
216-
elif leg._loc in [8]:
231+
elif stack == 'bottom':
217232
# stack to the bottom...
218233
layoutbox.vstack([child, leg._layoutbox], padding=paddingh)
219-
elif leg._loc in [9]:
234+
elif stack == 'top':
220235
# stack to the top...
221236
layoutbox.vstack([leg._layoutbox, child], padding=paddingh)
222237
self.figure.legends.append(leg)

0 commit comments

Comments
 (0)
0