Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eb2b3bb

Browse files
committed
select local var or one of multiple possible kwargs in descending order of priority
1 parent a6af675 commit eb2b3bb

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,12 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
11411141
if len(positions) == 0:
11421142
return []
11431143

1144+
# prevent 'singular' keys from **kwargs dict from overriding the effect
1145+
# of 'plural' keyword arguments (e.g. 'color' overriding 'colors')
1146+
colors = cbook.local_over_kwdict(colors, kwargs, 'color')
1147+
linewidths = cbook.local_over_kwdict(linewidths, kwargs, 'linewidth')
1148+
linestyles = cbook.local_over_kwdict(linestyles, kwargs, 'linestyle')
1149+
11441150
if not iterable(lineoffsets):
11451151
lineoffsets = [lineoffsets]
11461152
if not iterable(linelengths):

lib/matplotlib/cbook.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,62 @@ def __setstate__(self, state):
658658
self.extend(state['seq'])
659659

660660

661+
class IgnoredKeywordWarning(UserWarning):
662+
"""
663+
A class for issuing warnings about keyword arguments that will be ignored
664+
by matplotlib
665+
"""
666+
pass
667+
668+
669+
def local_over_kwdict(local_var, kwargs, *keys):
670+
"""
671+
Enforces the priority of a local variable over potentially conflicting
672+
argument(s) from a kwargs dict. The following possible output values are
673+
considered in order of priority:
674+
675+
local_var > kwargs[keys[0]] > ... > kwargs[keys[-1]]
676+
677+
The first of these whose value is not None will be returned. If all are
678+
None then None will be returned. Each key in keys will be removed from the
679+
kwargs dict in place.
680+
681+
Parameters
682+
------------
683+
local_var: any object
684+
The local variable (highest priority)
685+
686+
kwargs: dict
687+
Dictionary of keyword arguments; modified in place
688+
689+
keys: str(s)
690+
Name(s) of keyword arguments to process, in descending order of
691+
priority
692+
693+
Returns
694+
---------
695+
out: any object
696+
Either local_var or one of kwargs[key] for key in keys
697+
698+
Raises
699+
--------
700+
IgnoredKeywordWarning
701+
For each key in keys that is removed from kwargs but not used as
702+
the output value
703+
704+
"""
705+
out = local_var
706+
for key in keys:
707+
kwarg_val = kwargs.pop(key, None)
708+
if kwarg_val is not None:
709+
if out is None:
710+
out = kwarg_val
711+
else:
712+
warnings.warn('"%s" keyword argument will be ignored' % key,
713+
IgnoredKeywordWarning)
714+
return out
715+
716+
661717
def strip_math(s):
662718
'remove latex formatting from mathtext'
663719
remove = (r'\mathdefault', r'\rm', r'\cal', r'\tt', r'\it', '\\', '{', '}')

0 commit comments

Comments
 (0)