简体   繁体   中英

Set 'y' axis to scientific notation

I want my y axis to be formatted in scientific notation.

I have tried the matplotlib documentation , but it ignores my command.

import matplotlib.pyplot as plt
import numpy as np


x = np.random.randint(1e4, size=200)
y = np.random.randint(1e4, size=200)

plt.ticklabel_format(axis='both', style='sci')
plt.xlabel('x')
plt.ylabel('y')
plt.scatter(x,y, color='b', s=5, marker=".")

plt.show()

My output just appears in none-scientific notation.

For example I expect the ylabel 1000 to be 1E03 .

This code is just an example.

I have a sub-plot where plot 1 and 3 are in scientific notation, but plot 2 is in non-scientific notation.

Thanks.

You should just add scilimits=(4,4) to your command

plt.ticklabel_format(axis='both', style='sci', scilimits=(4,4))

for example your code will become:

x = np.random.randint(1e4,size=200)
y = np.random.randint(1e4,size=200)
plt.ticklabel_format(axis='both', style='sci', scilimits=(4,4))
plt.xlabel('x')
plt.ylabel('y')
plt.scatter(x,y, color='b', s=5, marker=".")
plt.show()

Passing

plt.ticklabel_format(axis='both', style='sci', scilimits=(0,0))

worked for me.

(as @Meysam-Sadeghi suggested, but without plt.subplots())

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