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

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