Skip to content
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

Merged
merged 7 commits into from Mar 30, 2015
26 changes: 15 additions & 11 deletions lib/matplotlib/axes/_axes.py
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

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

won't x.any() return False on np.zeros(5)? I think this should be .... if len(x) else ...

Copy link
Author

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

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)
Expand Down Expand Up @@ -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([[]])
Copy link
Member

Choose a reason for hiding this comment

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

Why a 2D array instead of 1D?

Copy link
Member

Choose a reason for hiding this comment

The 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:
Expand Down Expand Up @@ -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([])
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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()
Copy link
Member

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?

Copy link
Author

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!


n = []
mlast = None
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

This discards the _saved_bounds information. I am not strictly sure that this is the best place to do this, but this bit of code is convoluted enough....

Copy link
Member

Choose a reason for hiding this comment

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

Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 20 additions & 1 deletion lib/matplotlib/tests/test_axes.py
Expand Up @@ -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([], [])
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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():
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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
Expand Down