When I run this
import numpy as np
import matplotlib.pyplot as plt
plt.plot(np.arange(1e6, 3 * 1e7, 1e6))
I get this plot plot_before,
where the y-axis is a bit weird (there is a 1e7 on the top).
So, I am trying to fix this. I came up with a solution using FuncFormatter,
import numpy as np
import matplotlib.pyplot as plt
plt.plot(np.arange(1e6, 3 * 1e7, 1e6))
def y_fmt(x, y):
if x == 0:
return r'$0$'
r, p = "{:.1e}".format(x).split('e+')
r = r[:-2] if r[-1] == '0' else r
p = p[1:] if p[0] == '0' else p
return r'${:}\times10^{:}$'.format(r, p)
plt.gca().get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(y_fmt))
here is the result plot_after.
My question is, is there a better way of doing this, maybe using LogFormatterSciNotation? Or is it possible to say matplotlib to not put 1e7 on the top?
UPDATE:
I didn’t know that there is such a thing as
plt.ticklabel_format(useOffset=False)
but it seems that this is not doing anything for the data I used above (np.arange(1e6, 3 * 1e7, 1e6)). I don’t know if this is a bug or if there is something I don’t understand about this function...