Skip to content

Fix for plt.plot() does not support structured arrays as data= kwarg #10810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2018
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: 5 additions & 1 deletion lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ 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:
if (not args[1] in data and
not (hasattr(data, 'dtype') and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is correct, but a bit hard to read. I would just try ... except. The cleanest way would be a little helper function:

def _has_name(data, name):
    try:
        return name in data or name in data.dtype.names
    except (AttributeError, TypeError):
        return False

and then

if _has_name(data, args[1]):
    return ["y", "c"]

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"]
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,16 @@ 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)])

# 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)


@image_comparison(baseline_images=['const_xy'])
def test_const_xy():
fig = plt.figure()
Expand Down