Skip to content

Commit db76fc5

Browse files
authored
Merge pull request #28653 from tacaswell/mnt/generalize_plot_varargs
Mnt/generalize plot varargs
2 parents 3770dfe + ca2ecb2 commit db76fc5

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

lib/matplotlib/axes/_base.py

+24-12
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,9 @@ class _process_plot_var_args:
213213
an arbitrary number of *x*, *y*, *fmt* are allowed
214214
"""
215215

216-
def __init__(self, command='plot'):
217-
self.command = command
216+
def __init__(self, output='Line2D'):
217+
_api.check_in_list(['Line2D', 'Polygon', 'coordinates'], output=output)
218+
self.output = output
218219
self.set_prop_cycle(None)
219220

220221
def set_prop_cycle(self, cycler):
@@ -223,12 +224,12 @@ def set_prop_cycle(self, cycler):
223224
self._idx = 0
224225
self._cycler_items = [*cycler]
225226

226-
def __call__(self, axes, *args, data=None, **kwargs):
227+
def __call__(self, axes, *args, data=None, return_kwargs=False, **kwargs):
227228
axes._process_unit_info(kwargs=kwargs)
228229

229230
for pos_only in "xy":
230231
if pos_only in kwargs:
231-
raise _api.kwarg_error(self.command, pos_only)
232+
raise _api.kwarg_error(inspect.stack()[1].function, pos_only)
232233

233234
if not args:
234235
return
@@ -294,7 +295,9 @@ def __call__(self, axes, *args, data=None, **kwargs):
294295
this += args[0],
295296
args = args[1:]
296297
yield from self._plot_args(
297-
axes, this, kwargs, ambiguous_fmt_datakey=ambiguous_fmt_datakey)
298+
axes, this, kwargs, ambiguous_fmt_datakey=ambiguous_fmt_datakey,
299+
return_kwargs=return_kwargs
300+
)
298301

299302
def get_next_color(self):
300303
"""Return the next color in the cycle."""
@@ -329,13 +332,18 @@ def _setdefaults(self, defaults, kw):
329332
if kw.get(k, None) is None:
330333
kw[k] = defaults[k]
331334

332-
def _makeline(self, axes, x, y, kw, kwargs):
335+
def _make_line(self, axes, x, y, kw, kwargs):
333336
kw = {**kw, **kwargs} # Don't modify the original kw.
334337
self._setdefaults(self._getdefaults(kw), kw)
335338
seg = mlines.Line2D(x, y, **kw)
336339
return seg, kw
337340

338-
def _makefill(self, axes, x, y, kw, kwargs):
341+
def _make_coordinates(self, axes, x, y, kw, kwargs):
342+
kw = {**kw, **kwargs} # Don't modify the original kw.
343+
self._setdefaults(self._getdefaults(kw), kw)
344+
return (x, y), kw
345+
346+
def _make_polygon(self, axes, x, y, kw, kwargs):
339347
# Polygon doesn't directly support unitized inputs.
340348
x = axes.convert_xunits(x)
341349
y = axes.convert_yunits(y)
@@ -493,11 +501,15 @@ def _plot_args(self, axes, tup, kwargs, *,
493501
if y.ndim == 1:
494502
y = y[:, np.newaxis]
495503

496-
if self.command == 'plot':
497-
make_artist = self._makeline
498-
else:
504+
if self.output == 'Line2D':
505+
make_artist = self._make_line
506+
elif self.output == 'Polygon':
499507
kw['closed'] = kwargs.get('closed', True)
500-
make_artist = self._makefill
508+
make_artist = self._make_polygon
509+
elif self.output == 'coordinates':
510+
make_artist = self._make_coordinates
511+
else:
512+
_api.check_in_list(['Line2D', 'Polygon', 'coordinates'], output=self.output)
501513

502514
ncx, ncy = x.shape[1], y.shape[1]
503515
if ncx > 1 and ncy > 1 and ncx != ncy:
@@ -1299,7 +1311,7 @@ def __clear(self):
12991311
self._use_sticky_edges = True
13001312

13011313
self._get_lines = _process_plot_var_args()
1302-
self._get_patches_for_fill = _process_plot_var_args('fill')
1314+
self._get_patches_for_fill = _process_plot_var_args('Polygon')
13031315

13041316
self._gridOn = mpl.rcParams['axes.grid']
13051317
# Swap children to minimize time we spend in an invalid state

0 commit comments

Comments
 (0)