@@ -162,9 +162,12 @@ def _update_bbox_to_anchor(self, loc_in_canvas):
162
162
163
163
loc='upper right', bbox_to_anchor=(0.5, 0.5)
164
164
165
- ncol : int, default: 1
165
+ ncols : int, default: 1
166
166
The number of columns that the legend has.
167
167
168
+ For backward compatibility, the spelling *ncol* is also supported
169
+ but it is discouraged. If both are given, *ncols* takes precedence.
170
+
168
171
prop : None or `matplotlib.font_manager.FontProperties` or dict
169
172
The font properties of the legend. If None (default), the current
170
173
:data:`matplotlib.rcParams` will be used.
@@ -317,7 +320,7 @@ def __init__(
317
320
borderaxespad = None , # pad between the axes and legend border
318
321
columnspacing = None , # spacing between columns
319
322
320
- ncol = 1 , # number of columns
323
+ ncols = 1 , # number of columns
321
324
mode = None , # horizontal distribution of columns: None or "expand"
322
325
323
326
fancybox = None , # True: fancy box, False: rounded box, None: rcParam
@@ -333,6 +336,8 @@ def __init__(
333
336
frameon = None , # draw frame
334
337
handler_map = None ,
335
338
title_fontproperties = None , # properties for the legend title
339
+ * ,
340
+ ncol = 1 # synonym for ncols (backward compatibility)
336
341
):
337
342
"""
338
343
Parameters
@@ -418,8 +423,8 @@ def val_or_rc(val, rc_name):
418
423
419
424
handles = list (handles )
420
425
if len (handles ) < 2 :
421
- ncol = 1
422
- self ._ncol = ncol
426
+ ncols = 1
427
+ self ._ncols = ncols if ncols != 1 else ncol
423
428
424
429
if self .numpoints <= 0 :
425
430
raise ValueError ("numpoints must be > 0; it was %d" % numpoints )
@@ -581,6 +586,10 @@ def _set_loc(self, loc):
581
586
self .stale = True
582
587
self ._legend_box .set_offset (self ._findoffset )
583
588
589
+ def set_ncols (self , ncols ):
590
+ """Set the number of columns."""
591
+ self ._ncols = ncols
592
+
584
593
def _get_loc (self ):
585
594
return self ._loc_real
586
595
@@ -767,12 +776,12 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
767
776
handles_and_labels .append ((handlebox , textbox ))
768
777
769
778
columnbox = []
770
- # array_split splits n handles_and_labels into ncol columns, with the
771
- # first n%ncol columns having an extra entry. filter(len, ...) handles
772
- # the case where n < ncol : the last ncol -n columns are empty and get
773
- # filtered out.
774
- for handles_and_labels_column \
775
- in filter ( len , np .array_split (handles_and_labels , self ._ncol )):
779
+ # array_split splits n handles_and_labels into ncols columns, with the
780
+ # first n%ncols columns having an extra entry. filter(len, ...)
781
+ # handles the case where n < ncols : the last ncols -n columns are empty
782
+ # and get filtered out.
783
+ for handles_and_labels_column in filter (
784
+ len , np .array_split (handles_and_labels , self ._ncols )):
776
785
# pack handlebox and labelbox into itembox
777
786
itemboxes = [HPacker (pad = 0 ,
778
787
sep = self .handletextpad * fontsize ,
0 commit comments