Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e25f29c

Browse files
committed
Merge pull request #3943 from tacaswell/legend_deprecate_removal
Legend deprecate removal + cleanup
2 parents c8ed7ba + 84045a2 commit e25f29c

3 files changed

Lines changed: 43 additions & 52 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Code Removal
2+
````````````
3+
4+
Legend
5+
------
6+
Removed handling of `loc` as a positional argument to `Legend`

lib/matplotlib/axes/_axes.py

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,16 @@ def legend(self, *args, **kwargs):
458458
handles = kwargs.pop('handles', None)
459459
labels = kwargs.pop('labels', None)
460460

461-
if handles is not None and labels is None:
461+
if (handles is not None or labels is not None) and len(args):
462+
warnings.warn("You have mixed positional and keyword "
463+
"arguments, some input will be "
464+
"discarded.")
465+
466+
# if got both handles and labels as kwargs, make same length
467+
if handles and labels:
468+
handles, labels = zip(*zip(handles, labels))
469+
470+
elif handles is not None and labels is None:
462471
labels = [handle.get_label() for handle in handles]
463472
for label, handle in zip(labels[:], handles[:]):
464473
if label.startswith('_'):
@@ -470,7 +479,7 @@ def legend(self, *args, **kwargs):
470479

471480
elif labels is not None and handles is None:
472481
# Get as many handles as there are labels.
473-
handles = [handle for handle, _
482+
handles = [handle for handle, label
474483
in zip(self._get_legend_handles(handlers), labels)]
475484

476485
# No arguments - automatically detect labels and handles.
@@ -485,32 +494,13 @@ def legend(self, *args, **kwargs):
485494
elif len(args) == 1:
486495
labels, = args
487496
# Get as many handles as there are labels.
488-
handles = [handle for handle, _
497+
handles = [handle for handle, label
489498
in zip(self._get_legend_handles(handlers), labels)]
490499

491-
# Two arguments. Either:
500+
# Two arguments:
492501
# * user defined handles and labels
493-
# * user defined labels and location (deprecated)
494502
elif len(args) == 2:
495-
if is_string_like(args[1]) or isinstance(args[1], int):
496-
cbook.warn_deprecated('1.4', 'The "loc" positional argument '
497-
'to legend is deprecated. Please use '
498-
'the "loc" keyword instead.')
499-
labels, loc = args
500-
handles = [handle for handle, _
501-
in zip(self._get_legend_handles(handlers), labels)]
502-
kwargs['loc'] = loc
503-
else:
504-
handles, labels = args
505-
506-
# Three arguments. User defined handles, labels and
507-
# location (deprecated).
508-
elif len(args) == 3:
509-
cbook.warn_deprecated('1.4', 'The "loc" positional argument '
510-
'to legend is deprecated. Please '
511-
'use the "loc" keyword instead.')
512-
handles, labels, loc = args
513-
kwargs['loc'] = loc
503+
handles, labels = args
514504

515505
else:
516506
raise TypeError('Invalid arguments to legend.')

lib/matplotlib/tests/test_legend.py

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -144,22 +144,6 @@ def test_legend_remove():
144144
class TestLegendFunction(object):
145145
# Tests the legend function on the Axes and pyplot.
146146

147-
deprecation_message = ('The "loc" positional argument '
148-
'to legend is deprecated. Please use '
149-
'the "loc" keyword instead.')
150-
151-
@cleanup
152-
def test_legend_label_loc_args(self):
153-
# Check the deprecated warning is created and that the appropriate
154-
# call to Legend is made. This wouldn't actually create a valid
155-
# legend as there is no artist to legendify, but that doesn't matter.
156-
with mock.patch('matplotlib.cbook.warn_deprecated') as deprecation:
157-
with mock.patch('matplotlib.legend.Legend') as Legend:
158-
plt.legend(['hello world'], 1)
159-
160-
deprecation.assert_called_with('1.4', self.deprecation_message)
161-
Legend.assert_called_with(plt.gca(), [], ['hello world'], loc=1)
162-
163147
@cleanup
164148
def test_old_legend_handler_interface(self):
165149
# Check the deprecated warning is created and that the appropriate
@@ -187,18 +171,6 @@ def __call__(self, legend, orig_handle, fontsize, handlebox):
187171
MatplotlibDeprecationWarning,
188172
stacklevel=1)
189173

190-
@cleanup
191-
def test_legend_handle_label_loc_args(self):
192-
# Check the deprecated warning is created and that the appropriate
193-
# call to Legend is made.
194-
lines = plt.plot(range(10))
195-
with mock.patch('matplotlib.cbook.warn_deprecated') as deprecation:
196-
with mock.patch('matplotlib.legend.Legend') as Legend:
197-
plt.legend(lines, ['hello world'], 1)
198-
199-
deprecation.assert_called_with('1.4', self.deprecation_message)
200-
Legend.assert_called_with(plt.gca(), lines, ['hello world'], loc=1)
201-
202174
@cleanup
203175
def test_legend_handle_label(self):
204176
lines = plt.plot(range(10))
@@ -229,6 +201,29 @@ def test_legend_handler_map(self):
229201
plt.legend(handler_map={'1': 2})
230202
handles_labels.assert_called_with({'1': 2})
231203

204+
@cleanup
205+
def test_kwargs(self):
206+
fig, ax = plt.subplots(1, 1)
207+
th = np.linspace(0, 2*np.pi, 1024)
208+
lns, = ax.plot(th, np.sin(th), label='sin', lw=5)
209+
lnc, = ax.plot(th, np.cos(th), label='cos', lw=5)
210+
with mock.patch('matplotlib.legend.Legend') as Legend:
211+
ax.legend(handles=(lnc, lns), labels=('a', 'b'))
212+
Legend.assert_called_with(ax, (lnc, lns), ('a', 'b'))
213+
214+
@cleanup
215+
def test_warn_args_kwargs(self):
216+
fig, ax = plt.subplots(1, 1)
217+
th = np.linspace(0, 2*np.pi, 1024)
218+
lns, = ax.plot(th, np.sin(th), label='sin', lw=5)
219+
lnc, = ax.plot(th, np.cos(th), label='cos', lw=5)
220+
with mock.patch('warnings.warn') as warn:
221+
ax.legend((lnc, lns), labels=('a', 'b'))
222+
223+
warn.assert_called_with("You have mixed positional and keyword "
224+
"arguments, some input will be "
225+
"discarded.")
226+
232227

233228
@image_comparison(baseline_images=['legend_stackplot'], extensions=['png'])
234229
def test_legend_stackplot():

0 commit comments

Comments
 (0)