@@ -176,7 +176,8 @@ def _normalize(key, size, axis): # Includes last index.
176
176
177
177
return SubplotSpec (self , num1 , num2 )
178
178
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 ):
180
181
"""
181
182
legend for this gridspec, offset from all the subplots.
182
183
@@ -205,18 +206,32 @@ def legend_outside(self, handles=None, labels=None, axs=None, **kwargs):
205
206
206
207
leg ._update_width_height ()
207
208
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
+
208
224
for child in self ._layoutbox .children :
209
225
if child ._is_subplotspec_layoutbox ():
210
- if leg ._loc in [1 , 4 , 5 , 7 ]:
211
- # stack to the right...
226
+ if stack == 'right' :
212
227
layoutbox .hstack ([child , leg ._layoutbox ], padding = paddingw )
213
- elif leg . _loc in [ 2 , 3 , 6 ] :
228
+ elif stack == 'left' :
214
229
# stack to the left...
215
230
layoutbox .hstack ([leg ._layoutbox , child ], padding = paddingw )
216
- elif leg . _loc in [ 8 ] :
231
+ elif stack == 'bottom' :
217
232
# stack to the bottom...
218
233
layoutbox .vstack ([child , leg ._layoutbox ], padding = paddingh )
219
- elif leg . _loc in [ 9 ] :
234
+ elif stack == 'top' :
220
235
# stack to the top...
221
236
layoutbox .vstack ([leg ._layoutbox , child ], padding = paddingh )
222
237
self .figure .legends .append (leg )
0 commit comments