Skip to content

Commit 1b0219e

Browse files
committed
Rename ncol parameter to ncols
1 parent 028f07c commit 1b0219e

File tree

6 files changed

+25
-17
lines changed

6 files changed

+25
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
``ncol`` keyword argument to ``legend`` renamed to ``ncols``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
The ``ncol`` keyword argument to `~.legend` for controlling the number of
5+
columns is renamed to ``ncols`` for consistency with the `` ncols`` and
6+
``nrows`` keywords of `~.subplots` and `~.GridSpec`. Hence, using
7+
``ncol`` is deprecated and instead ``ncols`` should be used.

lib/matplotlib/backends/qt_editor/figureoptions.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,12 @@ def apply_callback(data):
230230
# re-generate legend, if checkbox is checked
231231
if generate_legend:
232232
draggable = None
233-
ncol = 1
233+
ncols = 1
234234
if axes.legend_ is not None:
235235
old_legend = axes.get_legend()
236236
draggable = old_legend._draggable is not None
237-
ncol = old_legend._ncol
238-
new_legend = axes.legend(ncol=ncol)
237+
ncols = old_legend._ncols
238+
new_legend = axes.legend(ncols=ncols)
239239
if new_legend:
240240
new_legend.set_draggable(draggable)
241241

lib/matplotlib/legend.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def _update_bbox_to_anchor(self, loc_in_canvas):
162162
163163
loc='upper right', bbox_to_anchor=(0.5, 0.5)
164164
165-
ncol : int, default: 1
165+
ncols : int, default: 1
166166
The number of columns that the legend has.
167167
168168
prop : None or `matplotlib.font_manager.FontProperties` or dict
@@ -294,6 +294,7 @@ class Legend(Artist):
294294
def __str__(self):
295295
return "Legend"
296296

297+
@_api.rename_parameter("3.6", "ncol", "ncols")
297298
@_api.make_keyword_only("3.6", "loc")
298299
@_docstring.dedent_interpd
299300
def __init__(
@@ -317,7 +318,7 @@ def __init__(
317318
borderaxespad=None, # pad between the axes and legend border
318319
columnspacing=None, # spacing between columns
319320

320-
ncol=1, # number of columns
321+
ncols=1, # number of columns
321322
mode=None, # horizontal distribution of columns: None or "expand"
322323

323324
fancybox=None, # True: fancy box, False: rounded box, None: rcParam
@@ -418,8 +419,8 @@ def val_or_rc(val, rc_name):
418419

419420
handles = list(handles)
420421
if len(handles) < 2:
421-
ncol = 1
422-
self._ncol = ncol
422+
ncols = 1
423+
self._ncols = ncols
423424

424425
if self.numpoints <= 0:
425426
raise ValueError("numpoints must be > 0; it was %d" % numpoints)
@@ -767,12 +768,12 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
767768
handles_and_labels.append((handlebox, textbox))
768769

769770
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)):
771+
# array_split splits n handles_and_labels into ncols columns, with the
772+
# first n%ncols columns having an extra entry. filter(len, ...)
773+
# handles the case where n < ncols: the last ncols-n columns are empty
774+
# and get filtered out.
775+
for handles_and_labels_column in filter(
776+
len, np.array_split(handles_and_labels, self._ncols)):
776777
# pack handlebox and labelbox into itembox
777778
itemboxes = [HPacker(pad=0,
778779
sep=self.handletextpad * fontsize,

lib/matplotlib/tests/test_axes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4013,7 +4013,7 @@ def test_hist_stacked_bar():
40134013
fig, ax = plt.subplots()
40144014
ax.hist(d, bins=10, histtype='barstacked', align='mid', color=colors,
40154015
label=labels)
4016-
ax.legend(loc='upper right', bbox_to_anchor=(1.0, 1.0), ncol=1)
4016+
ax.legend(loc='upper right', bbox_to_anchor=(1.0, 1.0), ncols=1)
40174017

40184018

40194019
def test_hist_barstacked_bottom_unchanged():

lib/matplotlib/tests/test_legend.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def test_fancy():
147147
plt.errorbar(np.arange(10), np.arange(10), xerr=0.5,
148148
yerr=0.5, label='XX')
149149
plt.legend(loc="center left", bbox_to_anchor=[1.0, 0.5],
150-
ncol=2, shadow=True, title="My legend", numpoints=1)
150+
ncols=2, shadow=True, title="My legend", numpoints=1)
151151

152152

153153
@image_comparison(['framealpha'], remove_text=True,
@@ -189,7 +189,7 @@ def test_legend_expand():
189189
ax.plot(x, x - 50, 'o', label='y=-1')
190190
l2 = ax.legend(loc='right', mode=mode)
191191
ax.add_artist(l2)
192-
ax.legend(loc='lower left', mode=mode, ncol=2)
192+
ax.legend(loc='lower left', mode=mode, ncols=2)
193193

194194

195195
@image_comparison(['hatching'], remove_text=True, style='default')

lib/matplotlib/tests/test_offsetbox.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def test_expand_with_tight_layout():
117117
d2 = [2, 1]
118118
ax.plot(d1, label='series 1')
119119
ax.plot(d2, label='series 2')
120-
ax.legend(ncol=2, mode='expand')
120+
ax.legend(ncols=2, mode='expand')
121121

122122
fig.tight_layout() # where the crash used to happen
123123

0 commit comments

Comments
 (0)