0

I want to format my ticks to a certain number of significant figures, AND remove the automatic offset. For the latter I am using https://stackoverflow.com/a/6654046/1021819, and for the former I would use https://stackoverflow.com/a/25750438/1021819, i.e.

y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False)
ax.yaxis.set_major_formatter(y_formatter)

and

ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2e'))

How do I combine the FormatStrFormatter and useOffset syntax?

Community
  • 1
  • 1
jtlz2
  • 7,700
  • 9
  • 64
  • 114

1 Answers1

3

FormatStrFormatter doesn't use an offset, so by using your second format you automatically won't have an offset.

Compare the two subplots in this example

import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import numpy as np  

fig,(ax1,ax2)=plt.subplots(2)

ax1.plot(np.arange(10000,10010,1))

ax2.plot(np.arange(10000,10010,1))
ax2.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.4e'))

plt.show()

enter image description here

tmdavison
  • 64,360
  • 12
  • 187
  • 165
  • Thanks! Playing devil's advocate, what if I DID want an offset under this scheme? – jtlz2 Feb 04 '16 at 11:07
  • 1
    Using ```yaxis.set_major_formatter(mtick.FormatStrFormatter('%.4e'))``` worked for me. Do you know why ```plt.plot(x,y); ax=plt.gca(); ax.ticklabel_format(useOffset=False)``` would NOT remove the offset? Is something more than those three lines needed? – Joe Oct 17 '20 at 14:56