Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 433
Naz Avatar asked Jul 18 '19 11:07

Naz


Video Answer


1 Answers

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()
like image 161
Meysam Sadeghi Avatar answered Oct 20 '22 10:10

Meysam Sadeghi