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: 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():