Reputation: 495
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.
Upvotes: 17
Views: 43624
Reputation: 163
The other solutions didn't work for me (after the plot command) on Matplotlib 3.4.3, but what worked was (here for the y-axis):
# set the y axis ticks to 10^x
ax.yaxis.set_major_formatter(mpl.ticker.FuncFormatter(lambda x, _: '{:.0e}'.format(x)))
# set the y axis tick labels to 10^x
ax.set_yticklabels(['$10^{'+str(int(np.log10(y)))+'}$' for y in ax.get_yticks()])
Upvotes: 0
Reputation: 1603
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()
Upvotes: 16
Reputation: 166
Passing
plt.ticklabel_format(axis='both', style='sci', scilimits=(0,0))
worked for me.
(as @Meysam-Sadeghi suggested, but without plt.subplots())
Upvotes: 9