简体   繁体   中英

Scientific tick label notation tuning

I am not completely satisfied with the default scientific formatting for the tick labels. For example,

import numpy as np
import pylab

pylab.rcParams['text.usetex'] = True
pylab.ticklabel_format(style='sci')

pylab.figure(1)
pylab.clf()
pylab.scatter(range(10),np.linspace(-21.305,-21.300,10))
pylab.show()

gives a picture with ticklabels of the form

在此处输入图片说明

What's wrong in here is firstly that -2.1298x10^1 should be in my opinion -21.298 , and in general I would only like to see only exponents divisible with 3 ( 10^3 , 10^6 etc and in the case of 10^0 i would like to hide it as in -21.298 ).

Also, I would like to have instead of 0.000 and -0.002 and so on 0 and -2 so that the exponent 10^-3 is included to the -21.298 so that in total the ticklabels would be

   x 10^-3 - 21.298
 0
-2
-4
-6

Now, have I missed some easy way to do this? Thank you in advance.

You can achieve it using the ScalarFormatter :

from matplotlib.ticker import ScalarFormatter

sf = ScalarFormatter()
sf.set_scientific(True)
sf.set_powerlimits((-0.00001, 0.00001))

ax = pylab.gca()
ax.yaxis.set_major_formatter(sf)
pylab.show()

You can test what the formatter will do to a given number by passing the number to format_data() :

sf.format_data(0.002)
#'2{\\times}10^{-3}'

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