Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b7df247

Browse filesBrowse files
committed
Merge pull request matplotlib#4119 from umairidris/hist_hexbin_empty
ENH : Allow empty input to hist and hexbin closes matplotlib#3886
2 parents 9348a46 + 22c6c7f commit b7df247
Copy full SHA for b7df247

File tree

Expand file treeCollapse file tree

5 files changed

+31
-10
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+31
-10
lines changed

‎lib/matplotlib/axes/_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.py
+12-10Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3886,10 +3886,9 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
38863886
if extent is not None:
38873887
xmin, xmax, ymin, ymax = extent
38883888
else:
3889-
xmin = np.amin(x)
3890-
xmax = np.amax(x)
3891-
ymin = np.amin(y)
3892-
ymax = np.amax(y)
3889+
xmin, xmax = (np.amin(x), np.amax(x)) if len(x) else (0, 1)
3890+
ymin, ymax = (np.amin(y), np.amax(y)) if len(y) else (0, 1)
3891+
38933892
# to avoid issues with singular data, expand the min/max pairs
38943893
xmin, xmax = mtrans.nonsingular(xmin, xmax, expander=0.1)
38953894
ymin, ymax = mtrans.nonsingular(ymin, ymax, expander=0.1)
@@ -5652,12 +5651,14 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
56525651

56535652
# basic input validation
56545653
flat = np.ravel(x)
5655-
if len(flat) == 0:
5656-
raise ValueError("x must have at least one data point")
5654+
5655+
input_empty = len(flat) == 0
56575656

56585657
# Massage 'x' for processing.
56595658
# NOTE: Be sure any changes here is also done below to 'weights'
5660-
if isinstance(x, np.ndarray) or not iterable(x[0]):
5659+
if input_empty:
5660+
x = np.array([[]])
5661+
elif isinstance(x, np.ndarray) or not iterable(x[0]):
56615662
# TODO: support masked arrays;
56625663
x = np.asarray(x)
56635664
if x.ndim == 2:
@@ -5712,7 +5713,7 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
57125713
# If bins are not specified either explicitly or via range,
57135714
# we need to figure out the range required for all datasets,
57145715
# and supply that to np.histogram.
5715-
if not binsgiven:
5716+
if not binsgiven and not input_empty:
57165717
xmin = np.inf
57175718
xmax = -np.inf
57185719
for xi in x:
@@ -5917,17 +5918,18 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
59175918
if np.sum(m) > 0: # make sure there are counts
59185919
xmin = np.amin(m[m != 0])
59195920
# filter out the 0 height bins
5920-
xmin = max(xmin*0.9, minimum)
5921+
xmin = max(xmin*0.9, minimum) if not input_empty else minimum
59215922
xmin = min(xmin0, xmin)
59225923
self.dataLim.intervalx = (xmin, xmax)
59235924
elif orientation == 'vertical':
59245925
ymin0 = max(_saved_bounds[1]*0.9, minimum)
59255926
ymax = self.dataLim.intervaly[1]
5927+
59265928
for m in n:
59275929
if np.sum(m) > 0: # make sure there are counts
59285930
ymin = np.amin(m[m != 0])
59295931
# filter out the 0 height bins
5930-
ymin = max(ymin*0.9, minimum)
5932+
ymin = max(ymin*0.9, minimum) if not input_empty else minimum
59315933
ymin = min(ymin0, ymin)
59325934
self.dataLim.intervaly = (ymin, ymax)
59335935

Loading
Loading
Loading

‎lib/matplotlib/tests/test_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,12 @@ def test_hexbin_extent():
495495

496496
ax.hexbin(x, y, extent=[.1, .3, .6, .7])
497497

498+
@image_comparison(baseline_images=['hexbin_empty'], remove_text=True,
499+
extensions=['png'])
500+
def test_hexbin_empty():
501+
# From #3886: creating hexbin from empty dataset raises ValueError
502+
ax = plt.gca()
503+
ax.hexbin([], [])
498504

499505
@cleanup
500506
def test_hexbin_pickable():
@@ -1014,6 +1020,19 @@ def test_hist_log():
10141020
ax = fig.add_subplot(111)
10151021
ax.hist(data, fill=False, log=True)
10161022

1023+
@image_comparison(baseline_images=['hist_bar_empty'], remove_text=True,
1024+
extensions=['png'])
1025+
def test_hist_bar_empty():
1026+
# From #3886: creating hist from empty dataset raises ValueError
1027+
ax = plt.gca()
1028+
ax.hist([], histtype='bar')
1029+
1030+
@image_comparison(baseline_images=['hist_step_empty'], remove_text=True,
1031+
extensions=['png'])
1032+
def test_hist_step_empty():
1033+
# From #3886: creating hist from empty dataset raises ValueError
1034+
ax = plt.gca()
1035+
ax.hist([], histtype='step')
10171036

10181037
@image_comparison(baseline_images=['hist_steplog'], remove_text=True)
10191038
def test_hist_steplog():

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.