Show decimal places and scientific notation on the axis

This is really easy to do if you use the matplotlib.ticker.FormatStrFormatter as opposed to the LogFormatter. The following code will label everything with the format ‘%.2e’: import numpy as np import matplotlib.pyplot as plt import matplotlib.ticker as mtick fig = plt.figure() ax = fig.add_subplot(111) x = np.linspace(0, 300, 20) y = np.linspace(0,300, 20) y = … Read more

Is the most significant decimal digits precision that can be converted to binary and back to decimal without loss of significance 6 or 7.225?

These are talking about two slightly different things. The 7.2251 digits is the precision with which a number can be stored internally. For one example, if you did a computation with a double precision number (so you were starting with something like 15 digits of precision), then rounded it to a single precision number, the … Read more

Show decimal places and scientific notation on the axis of a matplotlib plot

This is really easy to do if you use the matplotlib.ticker.FormatStrFormatter as opposed to the LogFormatter. The following code will label everything with the format ‘%.2e’: import numpy as np import matplotlib.pyplot as plt import matplotlib.ticker as mtick fig = plt.figure() ax = fig.add_subplot(111) x = np.linspace(0, 300, 20) y = np.linspace(0,300, 20) y = … Read more

How to round a number to significant figures in Python

You can use negative numbers to round integers: >>> round(1234, -3) 1000.0 Thus if you need only most significant digit: >>> from math import log10, floor >>> def round_to_1(x): … return round(x, -int(floor(log10(abs(x))))) … >>> round_to_1(0.0232) 0.02 >>> round_to_1(1234243) 1000000.0 >>> round_to_1(13) 10.0 >>> round_to_1(4) 4.0 >>> round_to_1(19) 20.0 You’ll probably have to take care … Read more