Skip to content

Commit 2ba1c58

Browse files
committed
Cleanup and document _plot_args()
1 parent 4e4410d commit 2ba1c58

File tree

1 file changed

+56
-15
lines changed

1 file changed

+56
-15
lines changed

lib/matplotlib/axes/_base.py

+56-15
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,53 @@ def _makefill(self, x, y, kw, kwargs):
403403
return seg, kwargs
404404

405405
def _plot_args(self, tup, kwargs, return_kwargs=False):
406+
"""
407+
Process the arguments of ``plot([x], y, [fmt], **kwargs)`` calls.
408+
409+
This processes a single set of ([x], y, [fmt]) parameters; i.e. for
410+
``plot(x, y, x2, y2)`` it will be called twice. Once for (x, y) and
411+
once for (x2, y2).
412+
413+
x and y may be 2D and thus can still represent multiple datasets.
414+
415+
For multiple datasets, if the keyword argument *label* is a list, this
416+
will unpack the list and assign the individual labels to the datasets.
417+
418+
Parameters
419+
----------
420+
tup : tuple
421+
A tuple of the positional parameters. This can be one of
422+
423+
- (y,)
424+
- (x, y)
425+
- (y, fmt)
426+
- (x, y, fmt)
427+
428+
kwargs : dict
429+
The keyword arguments passed to ``plot()``.
430+
431+
return_kwargs : bool
432+
If true, return the effective keyword arguments after label
433+
unpacking as well.
434+
435+
Returns
436+
-------
437+
result
438+
If *return_kwargs* is false, a list of Artists representing the
439+
dataset(s).
440+
If *return_kwargs* is true, a list of (Artist, effective_kwargs)
441+
representing the dataset(s). See *return_kwargs*.
442+
The Artist is either `.Line2D` (if called from ``plot()``) or
443+
`.Polygon` otherwise.
444+
"""
406445
if len(tup) > 1 and isinstance(tup[-1], str):
407-
linestyle, marker, color = _process_plot_format(tup[-1])
408-
tup = tup[:-1]
446+
# xy is tup with fmt stripped (could still be (y,) only)
447+
*xy, fmt = tup
448+
linestyle, marker, color = _process_plot_format(fmt)
409449
elif len(tup) == 3:
410450
raise ValueError('third arg must be a format string')
411451
else:
452+
xy = tup
412453
linestyle, marker, color = None, None, None
413454

414455
# Don't allow any None value; these would be up-converted to one
@@ -417,16 +458,16 @@ def _plot_args(self, tup, kwargs, return_kwargs=False):
417458
raise ValueError("x, y, and format string must not be None")
418459

419460
kw = {}
420-
for k, v in zip(('linestyle', 'marker', 'color'),
421-
(linestyle, marker, color)):
422-
if v is not None:
423-
kw[k] = v
424-
425-
if len(tup) == 2:
426-
x = _check_1d(tup[0])
427-
y = _check_1d(tup[-1])
461+
for prop_name, val in zip(('linestyle', 'marker', 'color'),
462+
(linestyle, marker, color)):
463+
if val is not None:
464+
kw[prop_name] = val
465+
466+
if len(xy) == 2:
467+
x = _check_1d(xy[0])
468+
y = _check_1d(xy[1])
428469
else:
429-
x, y = index_of(tup[-1])
470+
x, y = index_of(xy[-1])
430471

431472
if self.axes.xaxis is not None:
432473
self.axes.xaxis.update_units(x)
@@ -445,10 +486,10 @@ def _plot_args(self, tup, kwargs, return_kwargs=False):
445486
y = y[:, np.newaxis]
446487

447488
if self.command == 'plot':
448-
func = self._makeline
489+
make_artist = self._makeline
449490
else:
450491
kw['closed'] = kwargs.get('closed', True)
451-
func = self._makefill
492+
make_artist = self._makefill
452493

453494
ncx, ncy = x.shape[1], y.shape[1]
454495
if ncx > 1 and ncy > 1 and ncx != ncy:
@@ -465,8 +506,8 @@ def _plot_args(self, tup, kwargs, return_kwargs=False):
465506
else:
466507
labels = [label] * n_datasets
467508

468-
result = (func(x[:, j % ncx], y[:, j % ncy], kw,
469-
{**kwargs, 'label': label})
509+
result = (make_artist(x[:, j % ncx], y[:, j % ncy], kw,
510+
{**kwargs, 'label': label})
470511
for j, label in enumerate(labels))
471512

472513
if return_kwargs:

0 commit comments

Comments
 (0)