Skip to content

Commit d0d36cb

Browse files
committed
Clarify the implementation of _process_plot_var_args.
Having an argument named `kwargs` that's not a dict but a tuple is just a good way to trip the reader. Fortunately _getdefaults and _setdefaults in only even called with a single kwargs, so we can replace it with a single non-varargs argument and simplify the code at the same time.
1 parent d72f069 commit d0d36cb

File tree

1 file changed

+15
-37
lines changed

1 file changed

+15
-37
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ def _process_plot_format(fmt):
124124
return linestyle, marker, color
125125

126126

127+
def _no_none(d):
128+
"""Return the dict *d*, minus entries with a value of None."""
129+
return {k: v for k, v in d.items() if v is not None}
130+
131+
127132
class _process_plot_var_args(object):
128133
"""
129134
Process variable length arguments to the plot command, so that
@@ -235,27 +240,14 @@ def _xy_from_xy(self, x, y):
235240
y = y[:, np.newaxis]
236241
return x, y
237242

238-
def _getdefaults(self, ignore, *kwargs):
243+
def _getdefaults(self, ignore, kw):
239244
"""
240-
Only advance the cycler if the cycler has information that
241-
is not specified in any of the supplied tuple of dicts.
242-
Ignore any keys specified in the `ignore` set.
243-
244-
Returns a copy of defaults dictionary if there are any
245-
keys that are not found in any of the supplied dictionaries.
246-
If the supplied dictionaries have non-None values for
247-
everything the property cycler has, then just return
248-
an empty dictionary. Ignored keys are excluded from the
249-
returned dictionary.
250-
245+
If some keys in the property cycle (excluding those in the set
246+
*ignore*) are absent or set to None in the dict *kw*, return a copy
247+
of the next entry in the property cycle, excluding keys in *ignore*.
248+
Otherwise, don't advance the property cycle, and return an empty dict.
251249
"""
252-
prop_keys = self._prop_keys
253-
if ignore is None:
254-
ignore = set()
255-
prop_keys = prop_keys - ignore
256-
257-
if any(all(kw.get(k, None) is None for kw in kwargs)
258-
for k in prop_keys):
250+
if self._prop_keys - ignore - {*_no_none(kw)}:
259251
# Need to copy this dictionary or else the next time around
260252
# in the cycle, the dictionary could be missing entries.
261253
default_dict = next(self.prop_cycler).copy()
@@ -265,22 +257,10 @@ def _getdefaults(self, ignore, *kwargs):
265257
default_dict = {}
266258
return default_dict
267259

268-
def _setdefaults(self, defaults, *kwargs):
269-
"""
270-
Given a defaults dictionary, and any other dictionaries,
271-
update those other dictionaries with information in defaults if
272-
none of the other dictionaries contains that information.
273-
274-
"""
275-
for k in defaults:
276-
if all(kw.get(k, None) is None for kw in kwargs):
277-
for kw in kwargs:
278-
kw[k] = defaults[k]
279-
280260
def _makeline(self, x, y, kw, kwargs):
281261
kw = {**kw, **kwargs} # Don't modify the original kw.
282-
default_dict = self._getdefaults(None, kw)
283-
self._setdefaults(default_dict, kw)
262+
default_dict = self._getdefaults(set(), kw)
263+
kw = {**default_dict, **_no_none(kw)}
284264
seg = mlines.Line2D(x, y, **kw)
285265
return seg
286266

@@ -308,7 +288,7 @@ def _makefill(self, x, y, kw, kwargs):
308288
# Doing it with both seems to mess things up in
309289
# various places (probably due to logic bugs elsewhere).
310290
default_dict = self._getdefaults(ignores, kw)
311-
self._setdefaults(default_dict, kw)
291+
kw = {**default_dict, **_no_none(kw)}
312292

313293
# Looks like we don't want "color" to be interpreted to
314294
# mean both facecolor and edgecolor for some reason.
@@ -323,7 +303,7 @@ def _makefill(self, x, y, kw, kwargs):
323303

324304
# To get other properties set from the cycler
325305
# modify the kwargs dictionary.
326-
self._setdefaults(default_dict, kwargs)
306+
kwargs = {**default_dict, **_no_none(kwargs)}
327307

328308
seg = mpatches.Polygon(np.column_stack((x, y)),
329309
facecolor=facecolor,
@@ -391,8 +371,6 @@ def _grab_next_args(self, *args, **kwargs):
391371

392372

393373
class _AxesBase(martist.Artist):
394-
"""
395-
"""
396374
name = "rectilinear"
397375

398376
_shared_x_axes = cbook.Grouper()

0 commit comments

Comments
 (0)