8000 retaining dtype of hist bins as int when data is int and no weighting is used by pharshalp · Pull Request #12802 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

retaining dtype of hist bins as int when data is int and no weighting is used #12802

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 2 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
4 changes: 3 additions & 1 deletion lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
0