From 31dbef650fbe76b079057eb6f3916d270b48e56d Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Fri, 23 Mar 2018 08:28:05 +0100 Subject: [PATCH 1/2] cleanup _plot_args_replacer logic --- lib/matplotlib/__init__.py | 5 ++++- lib/matplotlib/axes/_axes.py | 20 +++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index b2186d1ef762..884350a3ef36 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -1529,6 +1529,9 @@ def _replacer(data, key): following arguments are replaced by **data[]**: {replaced} + + Objects passed as **data** must support item access (``data[]``) and + membership test (`` in data``). """ @@ -1554,7 +1557,7 @@ def _add_data_doc(docstring, replace_names, replace_all_args): if docstring is None: docstring = '' else: - docstring = dedent(docstring) + docstring = dedent(docstring)git _repl = "" if replace_names is None: _repl = "* All positional and all keyword arguments." diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index a223c4e563bf..311dad725159 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -44,18 +44,24 @@ rcParams = matplotlib.rcParams +def _has_item(data, name): + """Return whether *data* can be item-accessed with *name*. + + This supports data with a dict-like interface (`in` checks item + availability) and with numpy.arrays. + """ + try: + return name in data or name in data.dtype.names + except (AttributeError, TypeError): + return False + + def _plot_args_replacer(args, data): if len(args) == 1: return ["y"] elif len(args) == 2: # this can be two cases: x,y or y,c - if (not args[1] in data and - not (hasattr(data, 'dtype') and - hasattr(data.dtype, 'names') and - data.dtype.names is not None and - args[1] in data.dtype.names)): - # this is not in data, so just assume that it is something which - # will not get replaced (color spec or array like). + if not _has_item(data, args[1]): return ["y", "c"] # it's data, but could be a color code like 'ro' or 'b--' # -> warn the user in that case... From e9f9190cfe06d436304e53757023d7bd616d02a4 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Sat, 31 Mar 2018 12:04:06 -0400 Subject: [PATCH 2/2] FIX: remove typo --- lib/matplotlib/__init__.py | 6 +++--- lib/matplotlib/axes/_axes.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 884350a3ef36..39469c17cff4 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -1529,8 +1529,8 @@ def _replacer(data, key): following arguments are replaced by **data[]**: {replaced} - - Objects passed as **data** must support item access (``data[]``) and + + Objects passed as **data** must support item access (``data[]``) and membership test (`` in data``). """ @@ -1557,7 +1557,7 @@ def _add_data_doc(docstring, replace_names, replace_all_args): if docstring is None: docstring = '' else: - docstring = dedent(docstring)git + docstring = dedent(docstring) _repl = "" if replace_names is None: _repl = "* All positional and all keyword arguments." diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 311dad725159..6bb9b91c81c8 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -51,9 +51,9 @@ def _has_item(data, name): availability) and with numpy.arrays. """ try: - return name in data or name in data.dtype.names - except (AttributeError, TypeError): - return False + return data.dtype.names is not None and name in data.dtype.names + except AttributeError: # not a numpy array + return name in data def _plot_args_replacer(args, data):