From f69580e1cfa75a1a0e9baff2fb64907987945d1a Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 26 Jul 2024 14:28:50 -0400 Subject: [PATCH 1/3] EMH: add ability to return just data from _plot_var_args --- lib/matplotlib/axes/_base.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 87f5dbd4a5fe..b4371b66c304 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -214,6 +214,7 @@ class _process_plot_var_args: """ def __init__(self, command='plot'): + _api.check_in_list(['plot', 'fill', 'mirror'], command=command) self.command = command self.set_prop_cycle(None) @@ -335,6 +336,11 @@ def _makeline(self, axes, x, y, kw, kwargs): seg = mlines.Line2D(x, y, **kw) return seg, kw + def _mirror(self, axes, x, y, kw, kwargs): + kw = {**kw, **kwargs} # Don't modify the original kw. + self._setdefaults(self._getdefaults(kw), kw) + return (x, y), kw + def _makefill(self, axes, x, y, kw, kwargs): # Polygon doesn't directly support unitized inputs. x = axes.convert_xunits(x) @@ -495,9 +501,13 @@ def _plot_args(self, axes, tup, kwargs, *, if self.command == 'plot': make_artist = self._makeline - else: + elif self.command == 'fill': kw['closed'] = kwargs.get('closed', True) make_artist = self._makefill + elif self.command == 'mirror': + make_artist = self._mirror + else: + _api.check_in_list(['plot', 'fill', 'mirror'], command=self.command) ncx, ncy = x.shape[1], y.shape[1] if ncx > 1 and ncy > 1 and ncx != ncy: From ccaaa4a8bbc801cde268133fb9161a5a60b37106 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 26 Jul 2024 16:47:31 -0400 Subject: [PATCH 2/3] ENH: thread kwarg to return kwargs through __call__ --- lib/matplotlib/axes/_base.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index b4371b66c304..020e70d0c369 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -224,7 +224,7 @@ def set_prop_cycle(self, cycler): self._idx = 0 self._cycler_items = [*cycler] - def __call__(self, axes, *args, data=None, **kwargs): + def __call__(self, axes, *args, data=None, return_kwargs=False, **kwargs): axes._process_unit_info(kwargs=kwargs) for pos_only in "xy": @@ -295,7 +295,9 @@ def __call__(self, axes, *args, data=None, **kwargs): this += args[0], args = args[1:] yield from self._plot_args( - axes, this, kwargs, ambiguous_fmt_datakey=ambiguous_fmt_datakey) + axes, this, kwargs, ambiguous_fmt_datakey=ambiguous_fmt_datakey, + return_kwargs=return_kwargs + ) def get_next_color(self): """Return the next color in the cycle.""" From ca2ecb20d2cfc99dbb6169fdcca3207e22ef9c20 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:29:50 +0200 Subject: [PATCH 3/3] Restructure _process_plot_varargs Distinguish variants by desired output instead of calling command. This gives a better terminology for the new coordinate output variant. The command input was also used to create an error message containing the calling function name. This has been replaced by introspection. --- lib/matplotlib/axes/_base.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 020e70d0c369..4c5b18e9e843 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -213,9 +213,9 @@ class _process_plot_var_args: an arbitrary number of *x*, *y*, *fmt* are allowed """ - def __init__(self, command='plot'): - _api.check_in_list(['plot', 'fill', 'mirror'], command=command) - self.command = command + def __init__(self, output='Line2D'): + _api.check_in_list(['Line2D', 'Polygon', 'coordinates'], output=output) + self.output = output self.set_prop_cycle(None) def set_prop_cycle(self, cycler): @@ -229,7 +229,7 @@ def __call__(self, axes, *args, data=None, return_kwargs=False, **kwargs): for pos_only in "xy": if pos_only in kwargs: - raise _api.kwarg_error(self.command, pos_only) + raise _api.kwarg_error(inspect.stack()[1].function, pos_only) if not args: return @@ -332,18 +332,18 @@ def _setdefaults(self, defaults, kw): if kw.get(k, None) is None: kw[k] = defaults[k] - def _makeline(self, axes, x, y, kw, kwargs): + def _make_line(self, axes, x, y, kw, kwargs): kw = {**kw, **kwargs} # Don't modify the original kw. self._setdefaults(self._getdefaults(kw), kw) seg = mlines.Line2D(x, y, **kw) return seg, kw - def _mirror(self, axes, x, y, kw, kwargs): + def _make_coordinates(self, axes, x, y, kw, kwargs): kw = {**kw, **kwargs} # Don't modify the original kw. self._setdefaults(self._getdefaults(kw), kw) return (x, y), kw - def _makefill(self, axes, x, y, kw, kwargs): + def _make_polygon(self, axes, x, y, kw, kwargs): # Polygon doesn't directly support unitized inputs. x = axes.convert_xunits(x) y = axes.convert_yunits(y) @@ -501,15 +501,15 @@ def _plot_args(self, axes, tup, kwargs, *, if y.ndim == 1: y = y[:, np.newaxis] - if self.command == 'plot': - make_artist = self._makeline - elif self.command == 'fill': + if self.output == 'Line2D': + make_artist = self._make_line + elif self.output == 'Polygon': kw['closed'] = kwargs.get('closed', True) - make_artist = self._makefill - elif self.command == 'mirror': - make_artist = self._mirror + make_artist = self._make_polygon + elif self.output == 'coordinates': + make_artist = self._make_coordinates else: - _api.check_in_list(['plot', 'fill', 'mirror'], command=self.command) + _api.check_in_list(['Line2D', 'Polygon', 'coordinates'], output=self.output) ncx, ncy = x.shape[1], y.shape[1] if ncx > 1 and ncy > 1 and ncx != ncy: @@ -1311,7 +1311,7 @@ def __clear(self): self._use_sticky_edges = True self._get_lines = _process_plot_var_args() - self._get_patches_for_fill = _process_plot_var_args('fill') + self._get_patches_for_fill = _process_plot_var_args('Polygon') self._gridOn = mpl.rcParams['axes.grid'] # Swap children to minimize time we spend in an invalid state