-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Fix for plt.plot() does not support structured arrays as data= kwarg #10810
Conversation
707ca97
to
73101a2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works as advertised, and I don't see that its breaking anything...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.
@tacaswell, do you want to stick one of the version 2 milestones on this? It is addressing a bug milestoned for 2.2, and it looks like a clean and simple fix. |
Inclined to not backport, this never worked (so not fixing a regression), is not a failure to import / segfault, and the user has a clear work around (just don't use the data kwarg with structured arrays). The issue here is that structured arrays support Does this change how things fail if you pass in keys that are not a color or in the container? |
@tacaswell, thank you, I had not looked into the situation in enough detail. It sounds like the real solution for fully supporting structured arrays would be to make |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... as per comments bu @tacaswell, this looks to need more thought. However, if it breaks data kwarg handling and passes our current tests, it means we are missing tests on this functionality.
Thanks for working on this @zhangeugenia . Despite my seemingly negative comments, I think this is definitely going in the right direction and appreciate your work! |
Hello! Thank you all for your time and feedback, I greatly appretiate it (especially since plot() is a very important function)! As @tacaswell mentioned, this PR does indeed change the behaviour of Out of the two methods that @efiring proposed to support structured arrays, the latter (to check the type of data, and invoke I do have a question regarding testing though - once the support for structured arrays has been implemented, if I were to add tests to test Thank you! |
I'm not filly parsing the issue here, so will drop out of review. However, I can look into it if core devs too busy. Just ping issue again
@zhangeugenia I think I would put direct tests of Going the subclass route might not be a good idea--I suspect it would be overkill, and it is probably much trickier than one might expect. Is there likely to be any other place where it would help enough to justify the fuss? All you need for option 1 is something like if not (args[1] in data
or hasattr(data, 'dtype') and args[1] in data.dtype.names): To be even safer, in case something comes through with a 'dtype' that is not a standard numpy dtype, you could insert |
Additional note: ndarrays, structured or not, do have a contains method, but it doesn't do what we need here. |
I like @efiring 's suggestion. I am hesitant to add direct tests of |
Thank you for your guidance! Indeed, a wrapper class would be a lot of work, I just thought it would reduce issues with using I will take @efiring 's suggestion and make the needed changes to |
fb20640
to
73101a2
Compare
Updated to use @efiring 's suggestion! A check for |
Looks good to me! |
Thank you, @zhangeugenia! |
@@ -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 |
There was a problem hiding this comment.
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"]
Thanks @zhangeugenia and congratulations on what is (I think?) your first merged Matplotlib PR 🎉 Hopefully we will hear from you again! |
PR Summary
Fix for issue #8818 - plot() is now be able to plot structured arrays passed in as data=... kwarg without incorrectly interpreting the data as a format string.
A small simple test case is included with this PR to check that plotting a structured array will not cause and exception (without the use of image comparison since it felt unneeded).
If any changes/modifications are needed, please let me know. Thank you!