I thought the psuedocode published by Lewis Fogden looked familiar, and we really once coded this pseudo-code in C ++ for the build routine (to determine axis symmetry). I quickly translated it into Python, not sure if this is like pretty()
in R, but I hope this helps or is useful to anyone.
import numpy as np
def nicenumber(x, round):
exp = np.floor(np.log10(x))
f = x / 10**exp
if round:
if f < 1.5:
nf = 1.
elif f < 3.:
nf = 2.
elif f < 7.:
nf = 5.
else:
nf = 10.
else:
if f <= 1.:
nf = 1.
elif f <= 2.:
nf = 2.
elif f <= 5.:
nf = 5.
else:
nf = 10.
return nf * 10.**exp
def pretty(low, high, n):
range = nicenumber(high - low, False)
d = nicenumber(range / (n-1), True)
miny = np.floor(low / d) * d
maxy = np.ceil (high / d) * d
return np.arange(miny, maxy+0.5*d, d)
This creates, for example:
pretty(0.5, 2.56, 10)
pretty(0.5, 25.6, 10)
pretty(0.5, 256, 10 )
pretty(0.5, 2560, 10)
[0,5 1,1 1,5 2,5 2,5].
[0. 5. 10. 15. 20. 25. 30..]
[0. 50. 100. 150. 200. 250. 300.]
[0. 500. 1000. 1500. 2000. 2500. 3000.]