Skip to content

Masking invalid x and/or weights in hist #9706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/users/next_whats_new/2017-11-09_hist-ignores-nans.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Histogram function now accepts nan values in input
--------------------------------------------------

The `~.Axes.hist` function now accepts nan values in both the *data* and
*weights* input. Previously this would just error. Now any invalid values
are simply ignored when calculating the histogram values.

In addition, masked arrays are now valid input for both *data* and *weights*.
20 changes: 18 additions & 2 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5893,7 +5893,9 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
----------
x : (n,) array or sequence of (n,) arrays
Input values, this takes either a single array or a sequence of
arrays which are not required to be of the same length
arrays which are not required to be of the same length.
Masked arrays or arrays with invalid values (e.g. ``nan``) are
allowed.

bins : integer or sequence or 'auto', optional
If an integer is given, ``bins + 1`` bin edges are calculated and
Expand Down Expand Up @@ -5949,7 +5951,8 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
only contributes its associated weight towards the bin count
(instead of 1). If *normed* or *density* is ``True``,
the weights are normalized, so that the integral of the density
over the range remains 1.
over the range remains 1. Masked arrays or arrays with invalid
values (e.g. ``nan``) are allowed.

Default is ``None``

Expand Down Expand Up @@ -6130,6 +6133,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
else:
w = [None] * nx

# Comparing shape of weights vs. x
if len(w) != nx:
raise ValueError('weights should have the same shape as x')

Expand All @@ -6138,6 +6142,18 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
raise ValueError(
'weights should have the same shape as x')

# Combine the masks from x[i] and w[i] (if applicable) into a single
# mask and apply it to both.
if not input_empty:
for i, (xi, wi) in enumerate(zip(x, w)):
xi = cbook.safe_masked_invalid(xi)
mask = xi.mask
if wi is not None:
wi = cbook.safe_masked_invalid(wi)
mask = mask | wi.mask
w[i] = np.ma.masked_array(wi, mask=mask)
x[i] = np.ma.masked_array(xi, mask=mask)

if color is None:
color = [self._get_lines.get_next_color() for i in xrange(nx)]
else:
Expand Down
7 changes: 7 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,13 @@ def test_barh_tick_label():
align='center')


def test_hist_nans():
# Check that histogram input data can include nans
fig, ax = plt.subplots()
ax.hist([1, 2, 1, 2, 3, np.nan],
weights=[1, 1, 1, np.nan, 1, 1])


@image_comparison(baseline_images=['hist_log'],
remove_text=True)
def test_hist_log():
Expand Down