From 73101a2552aac9b2527089782edf940bdbe1f688 Mon Sep 17 00:00:00 2001 From: zhangeugenia Date: Fri, 16 Mar 2018 03:14:12 -0400 Subject: [PATCH 1/2] bugfix for #8818 --- lib/matplotlib/axes/_axes.py | 20 +++++++++++--------- lib/matplotlib/tests/test_axes.py | 9 +++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 74c28306c3a7..dda01721bf05 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -55,22 +55,24 @@ def _plot_args_replacer(args, data): return ["y"] elif len(args) == 2: # this can be two cases: x,y or y,c - if not args[1] in data: - # this is not in data, so just assume that it is something which - # will not get replaced (color spec or array like). - return ["y", "c"] # it's data, but could be a color code like 'ro' or 'b--' # -> warn the user in that case... + try: _process_plot_format(args[1]) except ValueError: pass else: - warnings.warn( - "Second argument {!r} is ambiguous: could be a color spec but " - "is in data; using as data. Either rename the entry in data " - "or use three arguments to plot.".format(args[1]), - RuntimeWarning, stacklevel=3) + # arg can be parsed into colour; verify arg is not data + if not args[1] in data: + return ["y", "c"] + else: + warnings.warn( + "Second argument {!r} is ambiguous: could be a color spec " + "but is in data; using as data. Either rename the entry " + "in data or use three arguments to plot.".format(args[1]), + RuntimeWarning, stacklevel=3) + return ["x", "y"] elif len(args) == 3: return ["x", "y", "c"] diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index abd2b6675b48..30ff3dc867b3 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -584,6 +584,15 @@ def test_shaped_data(): plt.plot(xdata[:, 1], xdata[1, :], 'o') +def test_structured_data(): + # support for stuctured data + pts = np.array([(1, 1), (2, 2)], dtype=[("ones", float), ("twos", float)]) + + fig, ax = plt.subplots(2) + ax[0].plot("ones", "twos", data=pts) + ax[1].plot("ones", "twos", "r", data=pts) + + @image_comparison(baseline_images=['const_xy']) def test_const_xy(): fig = plt.figure() From 557ddea297be803aecdbe3efba96bf6a6f5d56a7 Mon Sep 17 00:00:00 2001 From: zhangeugenia Date: Thu, 22 Mar 2018 01:15:54 -0400 Subject: [PATCH 2/2] updated fix for #8818 --- lib/matplotlib/axes/_axes.py | 24 +++++++++++++----------- lib/matplotlib/tests/test_axes.py | 1 + 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index dda01721bf05..7308835fda2d 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -55,24 +55,26 @@ def _plot_args_replacer(args, data): 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). + return ["y", "c"] # it's data, but could be a color code like 'ro' or 'b--' # -> warn the user in that case... - try: _process_plot_format(args[1]) except ValueError: pass else: - # arg can be parsed into colour; verify arg is not data - if not args[1] in data: - return ["y", "c"] - else: - warnings.warn( - "Second argument {!r} is ambiguous: could be a color spec " - "but is in data; using as data. Either rename the entry " - "in data or use three arguments to plot.".format(args[1]), - RuntimeWarning, stacklevel=3) - + warnings.warn( + "Second argument {!r} is ambiguous: could be a color spec but " + "is in data; using as data. Either rename the entry in data " + "or use three arguments to plot.".format(args[1]), + RuntimeWarning, stacklevel=3) return ["x", "y"] elif len(args) == 3: return ["x", "y", "c"] diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 30ff3dc867b3..e8cb3a243fde 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -588,6 +588,7 @@ def test_structured_data(): # support for stuctured data pts = np.array([(1, 1), (2, 2)], dtype=[("ones", float), ("twos", float)]) + # this should not read second name as a format and raise ValueError fig, ax = plt.subplots(2) ax[0].plot("ones", "twos", data=pts) ax[1].plot("ones", "twos", "r", data=pts)