Skip to content

Mnt/generalize plot varargs #28653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,9 @@
an arbitrary number of *x*, *y*, *fmt* are allowed
"""

def __init__(self, command='plot'):
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):
Expand All @@ -223,12 +224,12 @@
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":
if pos_only in kwargs:
raise _api.kwarg_error(self.command, pos_only)
raise _api.kwarg_error(inspect.stack()[1].function, pos_only)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit wary of stack walking, but this is only in an case were we are already going to errer out and raise it is OK.


if not args:
return
Expand Down Expand Up @@ -294,7 +295,9 @@
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."""
Expand Down Expand Up @@ -329,13 +332,18 @@
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 _makefill(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

Check warning on line 344 in lib/matplotlib/axes/_base.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/axes/_base.py#L342-L344

Added lines #L342 - L344 were not covered by tests

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)
Expand Down Expand Up @@ -493,11 +501,15 @@
if y.ndim == 1:
y = y[:, np.newaxis]

if self.command == 'plot':
make_artist = self._makeline
else:
if self.output == 'Line2D':
make_artist = self._make_line
elif self.output == 'Polygon':
kw['closed'] = kwargs.get('closed', True)
make_artist = self._makefill
make_artist = self._make_polygon
elif self.output == 'coordinates':
make_artist = self._make_coordinates

Check warning on line 510 in lib/matplotlib/axes/_base.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/axes/_base.py#L510

Added line #L510 was not covered by tests
else:
_api.check_in_list(['Line2D', 'Polygon', 'coordinates'], output=self.output)

Check warning on line 512 in lib/matplotlib/axes/_base.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/axes/_base.py#L512

Added line #L512 was not covered by tests

ncx, ncy = x.shape[1], y.shape[1]
if ncx > 1 and ncy > 1 and ncx != ncy:
Expand Down Expand Up @@ -1299,7 +1311,7 @@
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
Expand Down
Loading