Can someone give me a hint if I have an precission issue with floating points as stated here: Hist Precission with floats or do I miss something here??
import numpy
import matplotlib.pyplot as plt
frame = numpy.array([0.9, 0.99, 1, 1.9, 1.99, 2])
MyBins = numpy.linspace(0, 100, 1000, endpoint=False)
(counts, bins, bars) = plt.hist(frame, bins = MyBins)
Output counts:
0
... ...
8 0.0
9 2.0
10 1.0
... ...
18 1.0
19 1.0
20 1.0
The counts of 0.9 and 0.99 should be 2 as well as for 1.9 and 1.99. However, 1.9 is counted as an 1.8 value... If this an floating precission issue, what can I do to fix it?
Thank you!
CodePudding user response:
If you print the numbers with high precision they turn out to be something like: '0.1000000000000000055511151'
and '1.8999999999999999111821580'
. So yes, floating point precision likely is the issue. Moving your bin boundaries might be a solution.
Normally floating point numbers with the same precision should compare equal and behave as expected though. If your values and the histogram use different precisions, you might get unexpected results like this. You might try to find out what precision the histogram uses and use the same for your values.
Example:
frame = numpy.array([0.9, 0.99, 1, 1.9, 1.99, 2], dtype=numpy.float32)