Skip to content

Commit 503012d

Browse files
committed
FIX: restore (and test) handling of nan in hist data
1 parent b149eb6 commit 503012d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6666,6 +6666,14 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
66666666
_w = np.concatenate(w)
66676667
else:
66686668
_w = None
6669+
if bin_range is None:
6670+
xmin = np.inf
6671+
xmax = -np.inf
6672+
for xd in x:
6673+
xmin = np.min(xmin, np.nanmin(xd))
6674+
xmax = np.max(xmax, np.nanmax(xd))
6675+
bin_range = (xmin, xmax)
6676+
66696677
bins = histogram_bin_edges(np.concatenate(x),
66706678
bins, bin_range, _w)
66716679
else:

lib/matplotlib/tests/test_axes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6329,3 +6329,17 @@ def test_hist_auto_bins():
63296329
_, bins, _ = plt.hist([[1, 2, 3], [3, 4, 5, 6]], bins='auto')
63306330
assert bins[0] <= 1
63316331
assert bins[-1] >= 6
6332+
6333+
6334+
def test_hist_nan_data():
6335+
fig, (ax1, ax2) = plt.subplots(2)
6336+
6337+
data = [1, 2, 3]
6338+
nan_data = data + [np.nan]
6339+
6340+
bins, edges, _ = ax1.hist(data)
6341+
with np.errstate(invalid='ignore'):
6342+
nanbins, nanedges, _ = ax2.hist(nan_data)
6343+
6344+
assert np.allclose(bins, nanbins)
6345+
assert np.allclose(edges, nanedges)

0 commit comments

Comments
 (0)