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 ValueError being raised when plotting hist and hexbin on empty dataset (Fix #3886) #4119
Changes from 6 commits
e864d94
287a815
6deba68
74a4266
a567658
2bf9777
22c6c7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3846,10 +3846,9 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None, | |
if extent is not None: | ||
xmin, xmax, ymin, ymax = extent | ||
else: | ||
xmin = np.amin(x) | ||
xmax = np.amax(x) | ||
ymin = np.amin(y) | ||
ymax = np.amax(y) | ||
xmin, xmax = (np.amin(x), np.amax(x)) if x.any() else (0, 1) | ||
ymin, ymax = (np.amin(y), np.amax(y)) if y.any() else (0, 1) | ||
|
||
# to avoid issues with singular data, expand the min/max pairs | ||
xmin, xmax = mtrans.nonsingular(xmin, xmax, expander=0.1) | ||
ymin, ymax = mtrans.nonsingular(ymin, ymax, expander=0.1) | ||
|
@@ -5606,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: | ||
raise ValueError("x must have at least one data point") | ||
|
||
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.array([[]]) | ||
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. Why a 2D array instead of 1D? 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. never mind, I see why. |
||
elif isinstance(x, np.ndarray) or not iterable(x[0]): | ||
# TODO: support masked arrays; | ||
x = np.asarray(x) | ||
if x.ndim == 2: | ||
|
@@ -5640,7 +5641,9 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None, | |
|
||
# We need to do to 'weights' what was done to 'x' | ||
if weights is not None: | ||
if isinstance(weights, np.ndarray) or not iterable(weights[0]): | ||
if input_empty: | ||
w = np.array([]) | ||
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. This seems wrong to me as it is discarding the user input in the case of empty weights. I don't think this block of changes is required 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. Removed this block via 22c6c7f |
||
elif isinstance(weights, np.ndarray) or not iterable(weights[0]): | ||
w = np.array(weights) | ||
if w.ndim == 2: | ||
w = w.T | ||
|
@@ -5678,7 +5681,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() | ||
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. Why is this change needed? 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. 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! |
||
|
||
n = [] | ||
mlast = None | ||
|
@@ -5871,17 +5874,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) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -482,6 +482,12 @@ def test_hexbin_extent(): | |
|
||
ax.hexbin(x, y, extent=[.1, .3, .6, .7]) | ||
|
||
@image_comparison(baseline_images=['hexbin_empty'], remove_text=True, | ||
extensions=['png']) | ||
def test_hexbin_empty(): | ||
# From #3886: creating hexbin from empty dataset raises ValueError | ||
ax = plt.gca() | ||
ax.hexbin([], []) | ||
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. You need to create the Axes first. Also, I think you will want to use the @cleanup decorator. 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. Thanks! This is now done via commit cb8539f. |
||
|
||
@cleanup | ||
def test_hexbin_pickable(): | ||
|
@@ -1001,6 +1007,19 @@ def test_hist_log(): | |
ax = fig.add_subplot(111) | ||
ax.hist(data, fill=False, log=True) | ||
|
||
@image_comparison(baseline_images=['hist_bar_empty'], remove_text=True, | ||
extensions=['png']) | ||
def test_hist_bar_empty(): | ||
# From #3886: creating hist from empty dataset raises ValueError | ||
ax = plt.gca() | ||
ax.hist([], histtype='bar') | ||
|
||
@image_comparison(baseline_images=['hist_step_empty'], remove_text=True, | ||
extensions=['png']) | ||
def test_hist_step_empty(): | ||
# From #3886: creating hist from empty dataset raises ValueError | ||
ax = plt.gca() | ||
ax.hist([], histtype='step') | ||
|
||
@image_comparison(baseline_images=['hist_steplog'], remove_text=True) | ||
def test_hist_steplog(): | ||
|
@@ -3508,7 +3527,7 @@ def test_color_None(): | |
def test_numerical_hist_label(): | ||
fig, ax = plt.subplots() | ||
ax.hist([range(15)] * 5, label=range(5)) | ||
|
||
@cleanup | ||
def test_move_offsetlabel(): | ||
data = np.random.random(10) * 1e-22 | ||
|
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.
won't
x.any()
returnFalse
onnp.zeros(5)
? I think this should be.... if len(x) else ...
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.
Good catch! Fixed via 22c6c7f