8000 Merge pull request #23198 from oscargus/renamencol · matplotlib/matplotlib@92893d8 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 92893d8

Browse files
authored
Merge pull request #23198 from oscargus/renamencol
2 parents 973cf88 + 958e329 commit 92893d8

File tree

6 files changed

+43
-18
lines changed

6 files changed

+43
-18
lines changed
Lines changed: 7 additions & 0 deletions
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 `~.Axes.legend` for controlling the number of
5+
columns is renamed to ``ncols`` for consistency with the ``ncols`` and
6+
``nrows`` keywords of `~.Figure.subplots` and `~.GridSpec`.
7+
``ncol`` is still supported though.

lib/matplotlib/backends/qt_editor/figureoptions.py

Lines changed: 3 additions & 3 deletions
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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,12 @@ 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
168+
For backward compatibility, the spelling *ncol* is also supported
169+
but it is discouraged. If both are given, *ncols* takes precedence.
170+
168171
prop : None or `matplotlib.font_manager.FontProperties` or dict
169172
The font properties of the legend. If None (default), the current
170173
:data:`matplotlib.rcParams` will be used.
@@ -317,7 +320,7 @@ def __init__(
317320
borderaxespad=None, # pad between the axes and legend border
318321
columnspacing=None, # spacing between columns
319322

320-
ncol=1, # number of columns
323+
ncols=1, # number of columns
321324
mode=None, # horizontal distribution of columns: None or "expand"
322325

323326
fancybox=None, # True: fancy box, False: rounded box, None: rcParam
@@ -333,6 +336,8 @@ def __init__(
333336
frameon=None, # draw frame
334337
handler_map=None,
335338
title_fontproperties=None, # properties for the legend title
339+
*,
340+
ncol=1 # synonym for ncols (backward compatibility)
336341
):
337342
"""
338343
Parameters
@@ -418,8 +423,8 @@ def val_or_rc(val, rc_name):
418423

419424
handles = list(handles)
420425
if len(handles) < 2:
421-
ncol = 1
422-
self._ncol = ncol
426+
ncols = 1
427+
self._ncols = ncols if ncols != 1 else ncol
423428

424429
if self.numpoints <= 0:
425430
raise ValueError("numpoints must be > 0; it was %d" % numpoints)
@@ -581,6 +586,10 @@ def _set_loc(self, loc):
581586
self.stale = True
582587
self._legend_box.set_offset(self._findoffset)
583588

589+
def set_ncols(self, ncols):
590+
"""Set the number of columns."""
591+
self._ncols = ncols
592+
584593
def _get_loc(self):
585594
return self._loc_real
586595

@@ -767,12 +776,12 @@ def _init_legend_box(self, handles, labels, markerfirst=True):
767776
handles_and_labels.append((handlebox, textbox))
768777

769778
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)):
776785
# pack handlebox and labelbox into itembox
777786
itemboxes = [HPacker(pad=0,
778787
sep=self.handletextpad * fontsize,

lib/matplotlib/tests/test_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4045,7 +4045,7 @@ def test_hist_stacked_bar():
40454045
fig, ax = plt.subplots()
40464046
ax.hist(d, bins=10, histtype='barstacked', align='mid', color=colors,
40474047
label=labels)
4048-
ax.legend(loc='upper right', bbox_to_anchor=(1.0, 1.0), ncol=1)
4048+
ax.legend(loc='upper right', bbox_to_anchor=(1.0, 1.0), ncols=1)
40494049

40504050

40514051
def test_hist_barstacked_bottom_unchanged():

lib/matplotlib/tests/test_legend.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66
import pytest
77

8-
from matplotlib.testing.decorators import image_comparison
8+
from matplotlib.testing.decorators import check_figures_equal, image_comparison
99
from matplotlib.testing._markers import needs_usetex
1010
import matplotlib.pyplot as plt
1111
import matplotlib as mpl
@@ -148,7 +148,7 @@ def test_fancy():
148148
plt.errorbar(np.arange(10), np.arange(10), xerr=0.5,
149149
yerr=0.5, label='XX')
150150
plt.legend(loc="center left", bbox_to_anchor=[1.0, 0.5],
151-
ncol=2, shadow=True, title="My legend", numpoints=1)
151+
ncols=2, shadow=True, title="My legend", numpoints=1)
152152

153153

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

195195

196196
@image_comparison(['hatching'], remove_text=True, style='default')
@@ -926,3 +926,12 @@ def test_legend_markers_from_line2d():
926926

927927
assert markers == new_markers == _markers
928928
assert labels == new_labels
929+
930+
931+
@check_figures_equal()
932+
def test_ncol_ncols(fig_test, fig_ref):
933+
# Test that both ncol and ncols work
934+
strings = ["a", "b", "c", "d", "e", "f"]
935+
ncols = 3
936+
fig_test.legend(strings, ncol=ncols)
937+
fig_ref.legend(strings, ncols=ncols)

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 1 addition & 1 deletion
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)
0