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

Skip to content

Commit 27a648a

Browse files
committed
Merge pull request #4298 from alimuldal/color_prop_override
Prevent 'color' argument to eventplot from overriding 'colors' kwarg (fixes #4297)
2 parents e2315d2 + 8b083c1 commit 27a648a

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-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', '\\', '{', '}')

lib/matplotlib/tests/test_axes.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import matplotlib.markers as mmarkers
2121
from numpy.testing import assert_array_equal
2222
import warnings
23+
from matplotlib.cbook import IgnoredKeywordWarning
2324

2425

2526
@image_comparison(baseline_images=['formatter_ticker_001',
@@ -2317,6 +2318,37 @@ def test_eventplot_defaults():
23172318
colls = axobj.eventplot(data)
23182319

23192320

2321+
@image_comparison(baseline_images=['test_eventplot_problem_kwargs'], extensions=['png'], remove_text=True)
2322+
def test_eventplot_problem_kwargs():
2323+
'''
2324+
test that 'singular' versions of LineCollection props raise an
2325+
IgnoredKeywordWarning rather than overriding the 'plural' versions (e.g.
2326+
to prevent 'color' from overriding 'colors', see issue #4297)
2327+
'''
2328+
np.random.seed(0)
2329+
2330+
data1 = np.random.random([20]).tolist()
2331+
data2 = np.random.random([10]).tolist()
2332+
data = [data1, data2]
2333+
2334+
fig = plt.figure()
2335+
axobj = fig.add_subplot(111)
2336+
2337+
with warnings.catch_warnings(record=True) as w:
2338+
colls = axobj.eventplot(data,
2339+
colors=['r', 'b'],
2340+
color=['c', 'm'],
2341+
linewidths=[2, 1],
2342+
linewidth=[1, 2],
2343+
linestyles=['solid', 'dashed'],
2344+
linestyle=['dashdot', 'dotted'])
2345+
2346+
# check that three IgnoredKeywordWarnings were raised
2347+
assert_equal(len(w), 3)
2348+
assert_true(all(issubclass(wi.category, IgnoredKeywordWarning)
2349+
for wi in w))
2350+
2351+
23202352
@cleanup
23212353
def test_empty_eventplot():
23222354
fig, ax = plt.subplots(1, 1)

0 commit comments

Comments
 (0)