Skip to content

Commit bb2f30e

Browse files
committed
enforce priority of local variable, but fall back on kwarg if the local variable's value is None
1 parent a6af675 commit bb2f30e

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-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.kwarg_or_local(kwargs, 'color', colors)
1147+
linewidths = cbook.kwarg_or_local(kwargs, 'linewidth', linewidths)
1148+
linestyles = cbook.kwarg_or_local(kwargs, 'linestyle', linestyles)
1149+
11441150
if not iterable(lineoffsets):
11451151
lineoffsets = [lineoffsets]
11461152
if not iterable(linelengths):

lib/matplotlib/cbook.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,49 @@ 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 kwarg_or_local(kwargs, kwarg_name, local_var=None):
670+
"""
671+
Enforces the priority of a local variable over a potentially conflicting
672+
argument from a kwargs dict
673+
674+
Parameters:
675+
------------
676+
kwargs: dict
677+
Dictionary of keyword arguments; may be modified in place
678+
679+
kwarg_name: str
680+
Name of the keyword argument to process; kwargs[kwarg_name] will be
681+
removed in place if it exists
682+
683+
local_var: any object, optional
684+
Local variable
685+
686+
Returns:
687+
---------
688+
out: any object
689+
If local_var is None then kwargs[kwarg_name] will be returned,
690+
otherwise local_var will be returned and an IgnoredKeywordWarning
691+
will be raised
692+
693+
"""
694+
kwarg_val = kwargs.pop(kwarg_name, None)
695+
if kwarg_val is not None:
696+
if local_var is None:
697+
return kwarg_val
698+
else:
699+
warnings.warn('"%s" keyword argument will be ignored' % kwarg_name,
700+
IgnoredKeywordWarning)
701+
return local_var
702+
703+
661704
def strip_math(s):
662705
'remove latex formatting from mathtext'
663706
remove = (r'\mathdefault', r'\rm', r'\cal', r'\tt', r'\it', '\\', '{', '}')

0 commit comments

Comments
 (0)