Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,12 @@ def eventplot(self, positions, orientation='horizontal', lineoffsets=1,
if len(positions) == 0:
return []

# prevent 'singular' keys from **kwargs dict from overriding the effect
# of 'plural' keyword arguments (e.g. 'color' overriding 'colors')
colors = cbook.local_over_kwdict(colors, kwargs, 'color')
linewidths = cbook.local_over_kwdict(linewidths, kwargs, 'linewidth')
linestyles = cbook.local_over_kwdict(linestyles, kwargs, 'linestyle')

if not iterable(lineoffsets):
lineoffsets = [lineoffsets]
if not iterable(linelengths):
Expand Down
56 changes: 56 additions & 0 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,62 @@ def __setstate__(self, state):
self.extend(state['seq'])


class IgnoredKeywordWarning(UserWarning):
"""
A class for issuing warnings about keyword arguments that will be ignored
by matplotlib
"""
pass


def local_over_kwdict(local_var, kwargs, *keys):
"""
Enforces the priority of a local variable over potentially conflicting
argument(s) from a kwargs dict. The following possible output values are
considered in order of priority:

local_var > kwargs[keys[0]] > ... > kwargs[keys[-1]]

The first of these whose value is not None will be returned. If all are
None then None will be returned. Each key in keys will be removed from the
kwargs dict in place.

Parameters
------------
local_var: any object
The local variable (highest priority)

kwargs: dict
Dictionary of keyword arguments; modified in place

keys: str(s)
Name(s) of keyword arguments to process, in descending order of
priority

Returns
---------
out: any object
Either local_var or one of kwargs[key] for key in keys

Raises
--------
IgnoredKeywordWarning
For each key in keys that is removed from kwargs but not used as
the output value

"""
out = local_var
for key in keys:
kwarg_val = kwargs.pop(key, None)
if kwarg_val is not None:
if out is None:
out = kwarg_val
else:
warnings.warn('"%s" keyword argument will be ignored' % key,
IgnoredKeywordWarning)
return out


def strip_math(s):
'remove latex formatting from mathtext'
remove = (r'\mathdefault', r'\rm', r'\cal', r'\tt', r'\it', '\\', '{', '}')
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import matplotlib.markers as mmarkers
from numpy.testing import assert_array_equal
import warnings
from matplotlib.cbook import IgnoredKeywordWarning


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


@image_comparison(baseline_images=['test_eventplot_problem_kwargs'], extensions=['png'], remove_text=True)
def test_eventplot_problem_kwargs():
'''
test that 'singular' versions of LineCollection props raise an
IgnoredKeywordWarning rather than overriding the 'plural' versions (e.g.
to prevent 'color' from overriding 'colors', see issue #4297)
'''
np.random.seed(0)

data1 = np.random.random([20]).tolist()
data2 = np.random.random([10]).tolist()
data = [data1, data2]

fig = plt.figure()
axobj = fig.add_subplot(111)

with warnings.catch_warnings(record=True) as w:
colls = axobj.eventplot(data,
colors=['r', 'b'],
color=['c', 'm'],
linewidths=[2, 1],
linewidth=[1, 2],
linestyles=['solid', 'dashed'],
linestyle=['dashdot', 'dotted'])

# check that three IgnoredKeywordWarnings were raised
assert_equal(len(w), 3)
assert_true(all(issubclass(wi.category, IgnoredKeywordWarning)
for wi in w))


@cleanup
def test_empty_eventplot():
fig, ax = plt.subplots(1, 1)
Expand Down