Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5a2c9af

Browse files
committed
Allow empty datasets to be passed to hist, as long as at least one dataset has data
1 parent d1dd52a commit 5a2c9af

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

lib/matplotlib/axes.py

Lines changed: 14 additions & 6 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))

0 commit comments

Comments
 (0)