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

Skip to content

Commit c6f0554

Browse files
committed
FIX: restore (and test) handling of nan in hist data
1 parent 239be7b commit c6f0554

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@
3939
try:
4040
from numpy.lib.histograms import histogram_bin_edges
4141
except ImportError:
42+
# this function is new in np 1.15
4243
def histogram_bin_edges(arr, bins, range=None, weights=None):
44+
# this in True for 1D arrays, and False for None and str
45+
if np.ndim(bins) == 1:
46+
return bins
47+
4348
if isinstance(bins, str):
4449
# rather than backporting the internals, just do the full
4550
# computation. If this is too slow for users, they can
@@ -6630,9 +6635,6 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
66306635
if bin_range is not None:
66316636
bin_range = self.convert_xunits(bin_range)
66326637

6633-
# this in True for 1D arrays, and False for None and str
6634-
bins_array_given = np.ndim(bins) == 1
6635-
66366638
# We need to do to 'weights' what was done to 'x'
66376639
if weights is not None:
66386640
w = cbook._reshape_2D(weights, 'weights')
@@ -6659,14 +6661,32 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
66596661

66606662
hist_kwargs = dict()
66616663

6664+
# if the bin_range is not given, compute without nan numpy
6665+
# does not do this for us when guessing the range (but will
6666+
# happily ignore nans when computing the histogram).
6667+
if bin_range is None:
6668+
xmin = np.inf
6669+
xmax = -np.inf
6670+
for xi in x:
6671+
if len(xi):
6672+
# python's min/max ignore nan,
6673+
# np.minnan returns nan for all nan input
6674+
xmin = min(xmin, np.nanmin(xi))
6675+
xmax = max(xmax, np.nanmax(xi))
6676+
# make sure we have seen at least one non-nan and finite
6677+
# value before we reset the bin range
6678+
if not np.isnan([xmin, xmax]).any() and not (xmin > xmax):
6679+
bin_range = (xmin, xmax)
6680+
66626681
# If bins are not specified either explicitly or via range,
66636682
# we need to figure out the range required for all datasets,
66646683
# and supply that to np.histogram.
6665-
if not bins_array_given and not input_empty and len(x) > 1:
6684+
if not input_empty and len(x) > 1:
66666685
if weights is not None:
66676686
_w = np.concatenate(w)
66686687
else:
66696688
_w = None
6689+
66706690
bins = histogram_bin_edges(np.concatenate(x),
66716691
bins, bin_range, _w)
66726692
else:

lib/matplotlib/tests/test_axes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6352,3 +6352,17 @@ def test_hist_auto_bins():
63526352
_, bins, _ = plt.hist([[1, 2, 3], [3, 4, 5, 6]], bins='auto')
63536353
assert bins[0] <= 1
63546354
assert bins[-1] >= 6
6355+
6356+
6357+
def test_hist_nan_data():
6358+
fig, (ax1, ax2) = plt.subplots(2)
6359+
6360+
data = [1, 2, 3]
6361+
nan_data = data + [np.nan]
6362+
6363+
bins, edges, _ = ax1.hist(data)
6364+
with np.errstate(invalid='ignore'):
6365+
nanbins, nanedges, _ = ax2.hist(nan_data)
6366+
6367+
assert np.allclose(bins, nanbins)
6368+
assert np.allclose(edges, nanedges)

0 commit comments

Comments
 (0)