-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
FIX: Fix shape of hist output when input is multidimensional empty list #13368
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
Conversation
I'm not 100% following this PR. Why is the new behaviour better than the old? Does it really fix #13002? The test doesn't directly test |
lib/matplotlib/axes/_axes.py
Outdated
@@ -6573,7 +6573,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None, | |||
# basic input validation | |||
input_empty = np.size(x) == 0 | |||
# Massage 'x' for processing. | |||
if input_empty: | |||
if input_empty and len(x) == 0: |
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.
Or more simply, this entire if...else block can be deleted and replaced by x = cbook._reshape_2D(x, 'x')
which handles empty inputs just fine.
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.
That'll be much cleaner.
But it seems _reshape_2D
currently doesn't work with an empty list []
:
>>> mpl.cbook._reshape_2D([], 'x')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/hershen/matplotlib/lib/matplotlib/cbook/__init__.py", line 1418, in _reshape_2D
if X.ndim == 1 and not isinstance(X[0], collections.abc.Iterable):
IndexError: index 0 is out of bounds for axis 0 with size 0
Is it reasonable to modify it so that it returns [[]]
in such a case?
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.
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.
Well, I noticed it only because of your suggestion ;)
I don't mind waiting for #13392.
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.
It's fixed now.
I think the test is actually fine (it checks that we return a number of bar collections matching the number of inputs). Supporting empty inputs to hist() is like supporting empty inputs to plot(): "why wouldn't we?" |
@jklymak, I added context to the PR description. It's true that the test doesn't directly test that code. Should I add a test with that exact code? Currently empty list(s) to |
What happens now if you do a, _, _ = plt.hist([[], []])
print(a) yields
so this is an API change. Is it a good one? I guess so? At the very least needs an API note. |
7b81ca5
to
5c9f024
Compare
Right. With this PR, the output is: [array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]),
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])] Added an API change entry. |
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.
Thanks for humouring me @hershen
No worries @jklymak! |
c63012e
to
7d9857f
Compare
7d9857f
to
d99ed09
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.
anyone can merge post-ci
d99ed09
to
45f29b7
Compare
PR Summary
Fixes #13002.
Currently
plt.hist([np.array([])])
returns a single array of zeroes for the histogram values (n
in the documentation).There is some pre-processing that converts any input for which
np.size(input) == 0
into[np.array([])]
.In #13002 the code
plt.hist([[], []], color=["k", "r"])
produces an error because the input of[[],[]]
is pre-processed into[np.array([])]
and its length is no longer equal to the length ofcolor
.The fact that an input of
[[],[]]
is pre-processed in this way means that its output is a single array of bin values. This seems to contradict the documentation forn
:This PR modifies the treatment of multiple empty lists as input to follow the documentation. If the input contains multiple sets of data (even if they're empty), the output will contain the same number of histogram value sets. This also solves #13002.
PR Checklist