-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix ValueError being raised when plotting hist and hexbin on empty dataset (Fix #3886) #4119
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
Changes from 1 commit
e864d94
287a815
6deba68
74a4266
a567658
2bf9777
22c6c7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…step on empty input.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5394,7 +5394,7 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, | |
Compute and draw the histogram of *x*. The return value is a | ||
tuple (*n*, *bins*, *patches*) or ([*n0*, *n1*, ...], *bins*, | ||
[*patches0*, *patches1*,...]) if the input contains multiple | ||
data or ([], [], []) if input is an empty array. | ||
data. | ||
|
||
Multiple data can be provided via *x* as a list of datasets | ||
of potentially different length ([*x0*, *x1*, ...]), or as | ||
|
@@ -5605,12 +5605,14 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, | |
|
||
# basic input validation | ||
flat = np.ravel(x) | ||
if len(flat) == 0: | ||
return [], [], cbook.silent_list('No Patches') | ||
|
||
input_empty = len(flat) == 0 | ||
|
||
# Massage 'x' for processing. | ||
# NOTE: Be sure any changes here is also done below to 'weights' | ||
if isinstance(x, np.ndarray) or not iterable(x[0]): | ||
if input_empty: | ||
x = np.asarray([[]]) | ||
elif isinstance(x, np.ndarray) or not iterable(x[0]): | ||
# TODO: support masked arrays; | ||
x = np.asarray(x) | ||
if x.ndim == 2: | ||
|
@@ -5677,7 +5679,7 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, | |
#hist_kwargs = dict(range=range, normed=bool(normed)) | ||
# We will handle the normed kwarg within mpl until we | ||
# get to the point of requiring numpy >= 1.5. | ||
hist_kwargs = dict(range=bin_range) | ||
hist_kwargs = dict(range=bin_range) if not input_empty else dict() | ||
|
||
n = [] | ||
mlast = None | ||
|
@@ -5760,6 +5762,8 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, | |
for m, c in zip(n, color): | ||
if bottom is None: | ||
bottom = np.zeros(len(m), np.float) | ||
if input_empty: | ||
width = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per @efiring 's comment: "I think we should follow that logic, which implies that we should be drawing zero-height (or width) patches." Without setting width to 0, the patches for bar have size 0.1 and thus an empty graph (as desired) is not drawn. Before (width 0.1, non empty graph): However, currently the code still calculates the original width of 0.1 and then overwrites it, I will send a commit which moves it into a more appropriate place (where width, dw and boffset are originally calculated) in a future commit if this is the desired result. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the parenthetical widths comment was for drawing horizontal bars
and that the example with the line is correct.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done via 40cf945 |
||
if stacked: | ||
height = m - bottom | ||
else: | ||
|
@@ -5842,6 +5846,8 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, | |
xvals.append(x.copy()) | ||
yvals.append(y.copy()) | ||
|
||
fill_kwargs = dict() if not input_empty else dict(linewidth=0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again. do we need this? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Again. I think showing the line is the correct thing to do. My main motivation is that if some one now wants to update the histogram artists, this will cause a great deal of trouble. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just making sure, is autoscaling in the x still working even with On Fri, Mar 13, 2015 at 7:31 PM, Thomas A Caswell notifications@github.com
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done via 40cf945 |
||
|
||
if fill: | ||
# add patches in reverse order so that when stacking, | ||
# items lower in the stack are plottted on top of | ||
|
@@ -5850,14 +5856,16 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, | |
patches.append(self.fill( | ||
x, y, | ||
closed=True, | ||
facecolor=c)) | ||
facecolor=c, | ||
**fill_kwargs)) | ||
else: | ||
for x, y, c in reversed(list(zip(xvals, yvals, color))): | ||
split = 2 * len(bins) | ||
patches.append(self.fill( | ||
x[:split], y[:split], | ||
closed=False, edgecolor=c, | ||
fill=False)) | ||
fill=False, | ||
**fill_kwargs)) | ||
|
||
# we return patches, so put it back in the expected order | ||
patches.reverse() | ||
|
@@ -5870,17 +5878,18 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, | |
if np.sum(m) > 0: # make sure there are counts | ||
xmin = np.amin(m[m != 0]) | ||
# filter out the 0 height bins | ||
xmin = max(xmin*0.9, minimum) | ||
xmin = max(xmin*0.9, minimum) if not input_empty else minimum | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Yup, never mind my last comment. |
||
xmin = min(xmin0, xmin) | ||
self.dataLim.intervalx = (xmin, xmax) | ||
elif orientation == 'vertical': | ||
ymin0 = max(_saved_bounds[1]*0.9, minimum) | ||
ymax = self.dataLim.intervaly[1] | ||
|
||
for m in n: | ||
if np.sum(m) > 0: # make sure there are counts | ||
ymin = np.amin(m[m != 0]) | ||
# filter out the 0 height bins | ||
ymin = max(ymin*0.9, minimum) | ||
ymin = max(ymin*0.9, minimum) if not input_empty else minimum | ||
ymin = min(ymin0, ymin) | ||
self.dataLim.intervaly = (ymin, ymax) | ||
|
||
|
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.
Why is this change needed?
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.
The bin_range was (np.inf, -np.inf) when binsgiven was false due to the block above this line; I have changed that block to not be entered if input is empty and reverted this change via 22c6c7f.
Thanks!