8000 Merge pull request #2551 from neggert/hist_emptydata · cimarronm/matplotlib@45f5c45 · GitHub
[go: up one dir, main page]

Skip to content

Commit 45f5c45

Browse files
committed
Merge pull request matplotlib#2551 from neggert/hist_emptydata
Fix behavior of hist function when passed empty dataset
2 parents 0efcb84 + 7a5ec9f commit 45f5c45

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

lib/matplotlib/axes.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8245,6 +8245,17 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
82458245
if histtype == 'barstacked' and not stacked:
82468246
stacked = True
82478247

8248+
# Check whether bins or range are given explicitly.
8249+
binsgiven = (cbook.iterable(bins) or bin_range is not None)
8250+
8251+
# basic input validation
8252+
flat = np.ravel(x)
8253+
if len(flat) == 0:
8254+
raise ValueError("x must have at least one data point")
8255+
elif len(flat) == 1 and not binsgiven:
8256+
raise ValueError(
8257+
"x has only one data point. bins or range kwarg must be given")
8258+
82488259
# Massage 'x' for processing.
82498260
# NOTE: Be sure any changes here is also done below to 'weights'
82508261
if isinstance(x, np.ndarray) or not iterable(x[0]):
@@ -8299,19 +8310,16 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
82998310
# Save the datalimits for the same reason:
83008311
_saved_bounds = self.dataLim.bounds
83018312

8302-
# Check whether bins or range are given explicitly. In that
8303-
# case use those values for autoscaling.
8304-
binsgiven = (cbook.iterable(bins) or bin_range is not None)
8305-
83068313
# If bins are not specified either explicitly or via range,
83078314
# we need to figure out the range required for all datasets,
83088315
# and supply that to np.histogram.
83098316
if not binsgiven:
83108317
xmin = np.inf
83118318
xmax = -np.inf
83128319
for xi in x:
8313-
xmin = min(xmin, xi.min())
8314-
xmax = max(xmax, xi.max())
8320+
if len(xi) > 0:
8321+
xmin = min(xmin, xi.min())
8322+
xmax = max(xmax, xi.max())
83158323
bin_range = (xmin, xmax)
83168324

83178325
#hist_kwargs = dict(range=range, normed=bool(normed))
@@ -8503,15 +8511,17 @@ def hist(self, x, bins=10, range=None, normed=False, weights=None,
85038511
xmin0 = max(_saved_bounds[0]*0.9, minimum)
85048512
xmax = self.dataLim.intervalx[1]
85058513
for m in n:
8506-
xmin = np.amin(m[m != 0]) # filter out the 0 height bins
8514+
if np.sum(m) > 0: # make sure there are counts
8515+
xmin = np.amin(m[m != 0]) # filter out the 0 height bins
85078516
xmin = max(xmin*0.9, minimum)
85088517
xmin = min(xmin0, xmin)
85098518
self.dataLim.intervalx = (xmin, xmax)
85108519
elif orientation == 'vertical':
85118520
ymin0 = max(_saved_bounds[1]*0.9, minimum)
85128521
ymax = self.dataLim.intervaly[1]
85138522
for m in n:
8514-
ymin = np.amin(m[m != 0]) # filter out the 0 height bins
8523+
if np.sum(m) > 0: # make sure there are counts
8524+
ymin = np.amin(m[m != 0]) # filter out the 0 height bins
85158525
ymin = max(ymin*0.9, minimum)
85168526
ymin = min(ymin0, ymin)
85178527
self.dataLim.intervaly = (ymin, ymax)

lib/matplotlib/tests/test_axes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,12 @@ def test_hist_stacked_bar():
11621162
ax.hist(d, bins=10, histtype='barstacked', align='mid', color=colors, label=labels)
11631163
ax.legend(loc='upper right', bbox_to_anchor = (1.0, 1.0), ncol=1)
11641164

1165+
@cleanup
1166+
def test_hist_emptydata():
1167+
fig = plt.figure()
1168+
ax = fig.add_subplot(111)
1169+
ax.hist([[], range(10), range(10)], histtype="step")
1170+
11651171
@image_comparison(baseline_images=['transparent_markers'], remove_text=True)
11661172
def test_transparent_markers():
11671173
np.random.seed(0)

0 commit comments

Comments
 (0)
0