TheMeaningfulEngineer
TheMeaningfulEngineer

Reputation: 16359

Round error when plotting

I have the following array of data:

In [53]: data
Out[53]: array([  9.95000000e-05,   9.95000000e-05,   9.95000000e-05,
         ...
         ...
         9.95000000e-05])

When I do a plot of it i get:

enter image description here

I would expect the plot to be a straight line with something meaningful on the y axis. What is the cause for this behavior?

Upvotes: 1

Views: 111

Answers (1)

TheMeaningfulEngineer
TheMeaningfulEngineer

Reputation: 16359

The problem is that 9.95000000e-05 is actually 9.9500000000000006e-05 or 9.9499999999999979e-05 or some simmilar number which iPython rounds for the sake of clarity.

Matplotlib, however, acknowledge the number in its complete precision which as a result has the unexpected behaviour.

The workaround or this is to round the number to the values represented in iPython.

In [53]: round(data,7)
Out[53]: array([  9.95000000e-05,   9.95000000e-05,   9.95000000e-05,
         ... 
         ...
         ])

Which provides a nice plot:

In [54]: plot(round(data,7))

enter image description here

Upvotes: 3

Related Questions