Closed
Description
Bug summary
When creating a histogram out of float16 data, the bins are also calculated in float16. The lower precision can cause two errors:
- Gaps between certain bins.
- Two neighboring bins overlap each other (only visible when alpha < 1)
Code for reproduction
import numpy as np
import matplotlib.pyplot as plt
values = np.clip(np.random.normal(0.5, 0.3, size=1000), 0, 1).astype(np.float16)
plt.hist(values, bins=100, alpha=0.5)
plt.show()
Actual outcome
Expected outcome
Created by plt.hist(values.astype(np.float32), bins=100, alpha=0.5) plt.show()
Additional information
Possible solution
Calculate the bins in float32:
- Determine minimal and maximal value in float16.
- Convert min and max to float32.
- Calculate the bin edges.
Theoretical possible, but unwanted solution
Convert data into float32 before calculating the histogram. This behavior does not make a lot of sense, as float16 is mostly used because of memory limitations (arrays with billions of values can easily take several gigabytes).
Operating system
Windows 10
Matplotlib Version
3.4.3
Matplotlib Backend
TkAgg
Python version
3.7.1
Jupyter version
No response
Installation
pip