numpy – plus/minus operator for python ±

numpy – plus/minus operator for python ±

If you are looking to print the ± symbol, just use:


Another possibility: uncertainties is a module for doing calculations with error tolerances, ie

(2.1 +/- 0.05) + (0.6 +/- 0.05)    # => (2.7 +/- 0.1)

which would be written as

from uncertainties import ufloat

ufloat(2.1, 0.05) + ufloat(0.6, 0.05)

Edit: I was getting some odd results, and after a bit more playing with this I figured out why: the specified error is not a tolerance (hard additive limits as in engineering blueprints) but a standard-deviation value – which is why the above calculation results in

ufloat(2.7, 0.07071)    # not 0.1 as I expected!

numpy – plus/minus operator for python ±

If you happen to be using matplotlib, you can print mathematical expressions similar as one would with Latex. For the +/- symbol, you would use:

print( rvalue $pm$ error )

Where the r converts the string to a raw format and the $-signs are around the part of the string that is a mathematical equation. Any words that are in this part will be in a different font and will have no whitespace between them unless explicitly noted with the correct code. This can be found on the relavent page of the matplotlib documentation.

Sorry if this is too niche, but I stumbeled across this question trying to find this very answer.

Leave a Reply

Your email address will not be published. Required fields are marked *