8000 Merge pull request #26840 from anntzer/ppvars · matplotlib/matplotlib@ab1b6a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit ab1b6a3

Browse files
authored
Merge pull request #26840 from anntzer/ppvars
Reduce redundant information in _process_plot_var_args.
2 parents 3798de3 + ddad973 commit ab1b6a3

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ def set_prop_cycle(self, cycler):
228228
cycler = mpl.rcParams['axes.prop_cycle']
229229
self._idx = 0
230230
self._cycler_items = [*cycler]
231-
self._prop_keys = cycler.keys # This should make a copy
232231

233232
def __call__(self, axes, *args, data=None, **kwargs):
234233
axes._process_unit_info(kwargs=kwargs)
@@ -305,30 +304,27 @@ def __call__(self, axes, *args, data=None, **kwargs):
305304

306305
def get_next_color(self):
307306
"""Return the next color in the cycle."""
308-
if 'color' not in self._prop_keys:
309-
return 'k'
310-
c = self._cycler_items[self._idx]['color']
311-
self._idx = (self._idx + 1) % len(self._cycler_items)
312-
return c
307+
entry = self._cycler_items[self._idx]
308+
if "color" in entry:
309+
self._idx = (self._idx + 1) % len(self._cycler_items) # Advance cycler.
310+
return entry["color"]
311+
else:
312+
return "k"
313313

314-
def _getdefaults(self, ignore, kw):
314+
def _getdefaults(self, kw, ignore=frozenset()):
315315
"""
316316
If some keys in the property cycle (excluding those in the set
317317
*ignore*) are absent or set to None in the dict *kw*, return a copy
318318
of the next entry in the property cycle, excluding keys in *ignore*.
319319
Otherwise, don't advance the property cycle, and return an empty dict.
320320
"""
321-
prop_keys = self._prop_keys - ignore
322-
if any(kw.get(k, None) is None for k in prop_keys):
323-
# Need to copy this dictionary or else the next time around
324-
# in the cycle, the dictionary could be missing entries.
325-
default_dict = self._cycler_items[self._idx].copy()
326-
self._idx = (self._idx + 1) % len(self._cycler_items)
327-
for p in ignore:
328-
default_dict.pop(p, None)
321+
defaults = self._cycler_items[self._idx]
322+
if any(kw.get(k, None) is None for k in {*defaults} - ignore):
323+
self._idx = (self._idx + 1) % len(self._cycler_items) # Advance cycler.
324+
# Return a new dict to avoid exposing _cycler_items entries to mutation.
325+
return {k: v for k, v in defaults.items() if k not in ignore}
329326
else:
330-
default_dict = {}
331-
return default_dict
327+
return {}
332328

333329
def _setdefaults(self, defaults, kw):
334330
"""
@@ -341,8 +337,7 @@ def _setdefaults(self, defaults, kw):
341337

342338
def _makeline(self, axes, x, y, kw, kwargs):
343339
kw = {**kw, **kwargs} # Don't modify the original kw.
344-
default_dict = self._getdefaults(set(), kw)
345-
self._setdefaults(default_dict, kw)
340+
self._setdefaults(self._getdefaults(kw), kw)
346341
seg = mlines.Line2D(x, y, **kw)
347342
return seg, kw
348343

@@ -362,18 +357,16 @@ def _makefill(self, axes, x, y, kw, kwargs):
362357
# *user* explicitly specifies a marker which should be an error.
363358
# We also want to prevent advancing the cycler if there are no
364359
# defaults needed after ignoring the given properties.
365-
ignores = {'marker', 'markersize', 'markeredgecolor',
366-
'markerfacecolor', 'markeredgewidth'}
367-
# Also ignore anything provided by *kwargs*.
368-
for k, v in kwargs.items():
369-
if v is not None:
370-
ignores.add(k)
360+
ignores = ({'marker', 'markersize', 'markeredgecolor',
361+
'markerfacecolor', 'markeredgewidth'}
362+
# Also ignore anything provided by *kwargs*.
363+
| {k for k, v in kwargs.items() if v is not None})
371364

372365
# Only using the first dictionary to use as basis
373366
# for getting defaults for back-compat reasons.
374367
# Doing it with both seems to mess things up in
375368
# various places (probably due to logic bugs elsewhere).
376-
default_dict = self._getdefaults(ignores, kw)
369+
default_dict = self._getdefaults(kw, ignores)
377370
self._setdefaults(default_dict, kw)
378371

379372
# Looks like we don't want "color" to be interpreted to

0 commit comments

Comments
 (0)
0