Skip to content

Commit 1f7151e

Browse files
authored
Merge pull request #23047 from oscargus/histfix
2 parents 2304ae7 + 3473eca commit 1f7151e

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/matplotlib/axes/_axes.py

+1
Original file line numberDiff line numberDiff line change
@@ -6651,6 +6651,7 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
66516651
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
66526652
tops.append(m)
66536653
tops = np.array(tops, float) # causes problems later if it's an int
6654+
bins = np.array(bins, float) # causes problems if float16
66546655
if stacked:
66556656
tops = tops.cumsum(axis=0)
66566657
# If a stacked density plot, normalize so the area of all the

lib/matplotlib/tests/test_axes.py

+15
Original file line numberDiff line numberDiff line change
@@ -1863,6 +1863,21 @@ def test_hist_bar_empty():
18631863
ax.hist([], histtype='bar')
18641864

18651865

1866+
def test_hist_float16():
1867+
np.random.seed(19680801)
1868+
values = np.clip(
1869+
np.random.normal(0.5, 0.3, size=1000), 0, 1).astype(np.float16)
1870+
h = plt.hist(values, bins=3, alpha=0.5)
1871+
bc = h[2]
1872+
# Check that there are no overlapping rectangles
1873+
for r in range(1, len(bc)):
1874+
rleft = bc[r-1].get_corners()
1875+
rright = bc[r].get_corners()
1876+
# right hand position of left rectangle <=
1877+
# left hand position of right rectangle
1878+
assert rleft[1][0] <= rright[0][0]
1879+
1880+
18661881
@image_comparison(['hist_step_empty.png'], remove_text=True)
18671882
def test_hist_step_empty():
18681883
# From #3886: creating hist from empty dataset raises ValueError

0 commit comments

Comments
 (0)