Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: Gaps and overlapping areas between bins when using float16 #22622

Closed
@G-nn-r

Description

@G-nn-r

Bug summary

When creating a histogram out of float16 data, the bins are also calculated in float16. The lower precision can cause two errors:

  1. Gaps between certain bins.
  2. 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

float16

Expected outcome

float32

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions