Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

MNT: Separate property cycle handling from _process_plot_var_args #29469

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
Loading
from
Open
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
70 changes: 43 additions & 27 deletions 70 lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,42 @@
return linestyle, marker, color


class _PropCycle:
"""
A class that holds the property cycle information.

It expands the iterator-based Cycler into an explicit indexed list to support
conditional advancing.
"""
def __init__(self, cycler):
self._idx = 0
self._cycler_items = [*cycler]

def get_next_color(self):
"""Return the next color in the cycle."""
entry = self._cycler_items[self._idx]
if "color" in entry:
self._idx = (self._idx + 1) % len(self._cycler_items) # Advance cycler.
return entry["color"]
else:
return "k"

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

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/axes/_base.py#L223

Added line #L223 was not covered by tests

def getdefaults(self, kw, ignore=frozenset()):
"""
If some keys in the property cycle (excluding those in the set
*ignore*) are absent or set to None in the dict *kw*, return a copy
of the next entry in the property cycle, excluding keys in *ignore*.
Otherwise, don't advance the property cycle, and return an empty dict.
"""
defaults = self._cycler_items[self._idx]
if any(kw.get(k, None) is None for k in {*defaults} - ignore):
self._idx = (self._idx + 1) % len(self._cycler_items) # Advance cycler.
# Return a new dict to avoid exposing _cycler_items entries to mutation.
return {k: v for k, v in defaults.items() if k not in ignore}
else:
return {}


class _process_plot_var_args:
"""
Process variable length arguments to `~.Axes.plot`, to support ::
Expand All @@ -220,8 +256,7 @@
self.set_prop_cycle(None)

def set_prop_cycle(self, cycler):
self._idx = 0
self._cycler_items = [*mpl._val_or_rc(cycler, 'axes.prop_cycle')]
self._prop_cycle = _PropCycle(mpl._val_or_rc(cycler, 'axes.prop_cycle'))

def __call__(self, axes, *args, data=None, return_kwargs=False, **kwargs):
axes._process_unit_info(kwargs=kwargs)
Expand Down Expand Up @@ -300,29 +335,10 @@

def get_next_color(self):
"""Return the next color in the cycle."""
entry = self._cycler_items[self._idx]
if "color" in entry:
self._idx = (self._idx + 1) % len(self._cycler_items) # Advance cycler.
return entry["color"]
else:
return "k"

def _getdefaults(self, kw, ignore=frozenset()):
"""
If some keys in the property cycle (excluding those in the set
*ignore*) are absent or set to None in the dict *kw*, return a copy
of the next entry in the property cycle, excluding keys in *ignore*.
Otherwise, don't advance the property cycle, and return an empty dict.
"""
defaults = self._cycler_items[self._idx]
if any(kw.get(k, None) is None for k in {*defaults} - ignore):
self._idx = (self._idx + 1) % len(self._cycler_items) # Advance cycler.
# Return a new dict to avoid exposing _cycler_items entries to mutation.
return {k: v for k, v in defaults.items() if k not in ignore}
else:
return {}
return self._prop_cycle.get_next_color()

def _setdefaults(self, defaults, kw):
@staticmethod
Copy link
Member Author

@timhoffm timhoffm Jan 13, 2025

Choose a reason for hiding this comment

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

This is not strictly related, but noticed that _setdefaults does not depend on the class, so making that explicit via staticmethod as an improvement on the side.

def _setdefaults(defaults, kw):
"""
Add to the dict *kw* the entries in the dict *default* that are absent
or set to None in *kw*.
Expand All @@ -333,13 +349,13 @@

def _make_line(self, axes, x, y, kw, kwargs):
kw = {**kw, **kwargs} # Don't modify the original kw.
self._setdefaults(self._getdefaults(kw), kw)
self._setdefaults(self._prop_cycle.getdefaults(kw), kw)
seg = mlines.Line2D(x, y, **kw)
return seg, kw

def _make_coordinates(self, axes, x, y, kw, kwargs):
kw = {**kw, **kwargs} # Don't modify the original kw.
self._setdefaults(self._getdefaults(kw), kw)
self._setdefaults(self._prop_cycle.getdefaults(kw), kw)

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

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/axes/_base.py#L358

Added line #L358 was not covered by tests
return (x, y), kw

def _make_polygon(self, axes, x, y, kw, kwargs):
Expand Down Expand Up @@ -367,7 +383,7 @@
# for getting defaults for back-compat reasons.
# Doing it with both seems to mess things up in
# various places (probably due to logic bugs elsewhere).
default_dict = self._getdefaults(kw, ignores)
default_dict = self._prop_cycle.getdefaults(kw, ignores)
self._setdefaults(default_dict, kw)

# Looks like we don't want "color" to be interpreted to
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.