Skip to content

Fix issue with hist and float16 data #23047

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

Merged
merged 1 commit into from
May 16, 2022
Merged
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
1 change: 1 addition & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6651,6 +6651,7 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
tops.append(m)
tops = np.array(tops, float) # causes problems later if it's an int
bins = np.array(bins, float) # causes problems if float16
if stacked:
tops = tops.cumsum(axis=0)
# If a stacked density plot, normalize so the area of all the
Expand Down
15 changes: 15 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,21 @@ def test_hist_bar_empty():
ax.hist([], histtype='bar')


def test_hist_float16():
np.random.seed(19680801)
values = np.clip(
np.random.normal(0.5, 0.3, size=1000), 0, 1).astype(np.float16)
h = plt.hist(values, bins=3, alpha=0.5)
bc = h[2]
# Check that there are no overlapping rectangles
for r in range(1, len(bc)):
rleft = bc[r-1].get_corners()
rright = bc[r].get_corners()
# right hand position of left rectangle <=
# left hand position of right rectangle
assert rleft[1][0] <= rright[0][0]


@image_comparison(['hist_step_empty.png'], remove_text=True)
def test_hist_step_empty():
# From #3886: creating hist from empty dataset raises ValueError
Expand Down