Skip to content

Commit 5e8f2b8

Browse files
committed
Reduce redundant information in _process_plot_var_args.
_prop_keys is redundant with _cycler_items and not really needed anywhere. Removing it makes it less likely that a _process_plot_var_args gets in an invalid state if _cycler_items is updated while _prop_keys is not touched.
1 parent 0f3f768 commit 5e8f2b8

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 12 additions & 16 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,11 +304,12 @@ 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

314314
def _getdefaults(self, ignore, kw):
315315
"""
@@ -318,17 +318,13 @@ def _getdefaults(self, ignore, kw):
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
"""

0 commit comments

Comments
 (0)