简体   繁体   中英

Set custom scientific notation y tick on log-plot

I am trying to set custom ticks for my plots but I'm struggling a lot, I tried solution from several other thread and none works:/ I need to do two distinct plots and I have some issues on them.

I would like to have custom y ticks that does 5x10^-3, 2x10^-3, 1x10^-3, 5x10^-4... (multiple of 1,2,5 and in scientific notation)

On both plot I can't seem to modify the ticks. (I thought the issue for the second plot that it was because the minor ticks where invisible, but even after putting them visible I couldn't modify them). A solution where I set ticks "half worked", meaning that I couldn't set them in scientific notation

在此处输入图像描述 在此处输入图像描述

Any help would be very appreciated:)

plt.rc('text', usetex=True)  
plt.rc('font', family='serif')  
plt.rcParams['figure.dpi'] = 600
plt.rcParams["figure.figsize"] = (4,2.1)
matplotlib.rcParams.update({'font.size': 13})

plt.plot([100,200 ,300 ,400 ,500 ,1000,1500,2000],[0.001378,0.000817,0.000627,0.000533,0.000457,0.000317,0.000240,0.000206])   

plt.grid(axis='y', linestyle='dotted', which='minor')
plt.grid(axis='x',linestyle='dotted')
plt.yscale('log')
plt.yticks([0.00005])
plt.show()

I would remove your statement

plt.grid(axis='y', linestyle='dotted', which='minor')
plt.grid(axis='x',linestyle='dotted')
plt.yscale('log')
plt.yticks([0.00005])

and replace it by:

plt.yscale('log')
yticks = [5E-3, 2E-3, 1E-3, 5E-4, 2E-4, 1E-4]
plt.yticks(ticks=yticks,labels=[format(x, '.0e') for x in yticks])
plt.grid(axis='y')

Does it help?

Edit:

If you really want the label to be format as ax 10-b , you can use this bit of code:

yticks = [5E-3, 2E-3, 1E-3, 5E-4, 2E-4, 1E-4]
ylab=[]
for c,x in enumerate(yticks):
    xstr=format(x, '.0e').split('e')
    ylab.append(xstr[0]+ ' x $10^{' + xstr[1] +'}$' )
plt.yticks(ticks=yticks,labels=ylab)

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