简体   繁体   中英

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 , ie

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?

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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM