Skip to content

Commit 31dbef6

Browse files
committed
cleanup _plot_args_replacer logic
1 parent 5669261 commit 31dbef6

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

lib/matplotlib/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,9 @@ def _replacer(data, key):
15291529
following arguments are replaced by **data[<arg>]**:
15301530
15311531
{replaced}
1532+
1533+
Objects passed as **data** must support item access (``data[<arg>]``) and
1534+
membership test (``<arg> in data``).
15321535
"""
15331536

15341537

@@ -1554,7 +1557,7 @@ def _add_data_doc(docstring, replace_names, replace_all_args):
15541557
if docstring is None:
15551558
docstring = ''
15561559
else:
1557-
docstring = dedent(docstring)
1560+
docstring = dedent(docstring)git
15581561
_repl = ""
15591562
if replace_names is None:
15601563
_repl = "* All positional and all keyword arguments."

lib/matplotlib/axes/_axes.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,24 @@
4444
rcParams = matplotlib.rcParams
4545

4646

47+
def _has_item(data, name):
48+
"""Return whether *data* can be item-accessed with *name*.
49+
50+
This supports data with a dict-like interface (`in` checks item
51+
availability) and with numpy.arrays.
52+
"""
53+
try:
54+
return name in data or name in data.dtype.names
55+
except (AttributeError, TypeError):
56+
return False
57+
58+
4759
def _plot_args_replacer(args, data):
4860
if len(args) == 1:
4961
return ["y"]
5062
elif len(args) == 2:
5163
# this can be two cases: x,y or y,c
52-
if (not args[1] in data and
53-
not (hasattr(data, 'dtype') and
54-
hasattr(data.dtype, 'names') and
55-
data.dtype.names is not None and
56-
args[1] in data.dtype.names)):
57-
# this is not in data, so just assume that it is something which
58-
# will not get replaced (color spec or array like).
64+
if not _has_item(data, args[1]):
5965
return ["y", "c"]
6066
# it's data, but could be a color code like 'ro' or 'b--'
6167
# -> warn the user in that case...

0 commit comments

Comments
 (0)