From bff4e0cb8194b490ab11370e1485e61417b01210 Mon Sep 17 00:00:00 2001 From: pharshalp Date: Mon, 12 Nov 2018 18:20:18 -0500 Subject: [PATCH 1/2] postpone casting to float to allow hist vals to remain ints when the input data is ints and no weighting is used --- lib/matplotlib/axes/_axes.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 9037674780e2..ae0fb86cfeab 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -6635,7 +6635,6 @@ def hist(self, x, bins=None, range=None, density=None, weights=None, # this will automatically overwrite bins, # so that each histogram uses the same bins m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs) - m = m.astype(float) # causes problems later if it's an int if mlast is None: mlast = np.zeros(len(bins)-1, m.dtype) if stacked: @@ -6647,6 +6646,9 @@ def hist(self, x, bins=None, range=None, density=None, weights=None, # histograms together is 1 if stacked and density: db = np.diff(bins) + # casting as float to avoid issues with division result being + # stored in the same numpy array (in case the dtype is int). + tops = [m.astype(float) for m in tops] for m in tops: m[:] = (m / db) / tops[-1].sum() if cumulative: From 6565e495913114c9d5466ef2c37ec6b3431acbd4 Mon Sep 17 00:00:00 2001 From: pharshalp Date: Mon, 12 Nov 2018 18:48:52 -0500 Subject: [PATCH 2/2] Added test to check if hist of ints returns ints in no weighting --- lib/matplotlib/tests/test_axes.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index f6e3f733b95b..6851118338ba 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1492,6 +1492,12 @@ def test_barh_tick_label(): align='center') +def test_int_hist(): + fig, ax = plt.subplots() + n, _, _ = ax.hist(np.arange(10)) + assert n.dtype == np.int + + @image_comparison(baseline_images=['hist_log'], remove_text=True) def test_hist_log():