jtlz2
jtlz2

Reputation: 8407

matplotlib: format axis ticks without offset

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?

Upvotes: 0

Views: 1802

Answers (1)

tmdavison
tmdavison

Reputation: 69076

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

Upvotes: 3

Related Questions