Skip to content

Commit

Permalink
Merge pull request #4119 from umairidris/hist_hexbin_empty
Browse files Browse the repository at this point in the history
ENH : Allow empty input to hist and hexbin 

closes #3886
  • Loading branch information
tacaswell committed Mar 30, 2015
2 parents 9348a46 + 22c6c7f commit b7df247
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/matplotlib/axes/_axes.py
Expand Up @@ -3886,10 +3886,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 len(x) else (0, 1)
ymin, ymax = (np.amin(y), np.amax(y)) if len(y) 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 @@ -5652,12 +5651,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([[]])
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 @@ -5712,7 +5713,7 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
# If bins are not specified either explicitly or via range,
# we need to figure out the range required for all datasets,
# and supply that to np.histogram.
if not binsgiven:
if not binsgiven and not input_empty:
xmin = np.inf
xmax = -np.inf
for xi in x:
Expand Down Expand Up @@ -5917,17 +5918,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
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.
19 changes: 19 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Expand Up @@ -495,6 +495,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([], [])

@cleanup
def test_hexbin_pickable():
Expand Down Expand Up @@ -1014,6 +1020,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

0 comments on commit b7df247

Please sign in to comment.