2

The only way I know how to use scientific notation for the end of the axes in matplotlib is with

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

but this will use 1e instead of x10. In the example code below it shows 1e6, but I want x10 to the power of 6, x10superscript6 (x10^6 with the 6 small and no ^). Is there a way to do this?

edit: I do not want scientific notation for each tick in the axis (that does not look good imho), only at the end, like the example shows but with just the 1e6 part altered to x10superscript6.

I can't include images yet.

Thanks

import numpy as np
import matplotlib.pyplot as plt
plt.figure()
x = np.linspace(0,1000)
y = x**2
plt.plot(x,y)
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
plt.show()
  • 2
    Possible duplicate of [Can I show decimal places and scientific notation on the axis of a matplotlib plot using Python 2.7?](https://stackoverflow.com/questions/25750170/can-i-show-decimal-places-and-scientific-notation-on-the-axis-of-a-matplotlib-pl) – tnknepp Jan 24 '19 at 20:32
  • ImportannceOfBeingErnest showed how to do this here: https://stackoverflow.com/a/49330649/1678467 – tnknepp Jan 24 '19 at 20:33
  • Thanks, but maybe plotting sometimes looked different with python 2.7. I'm not looking for scientific notation on each tick, only at the end of the axis. It's odd I don't see questions about this already though, as I think x10 to the power of something may the default in other programs. –  Jan 24 '19 at 20:39
  • 1
    Are you looking for `plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0), useMathText=True)`? – ImportanceOfBeingErnest Jan 24 '19 at 20:46
  • @ImportanceOfBeingErnest That made the fix on my end. How you know EVERYTHING about python plotting I don't understand... – tnknepp Jan 24 '19 at 20:49
  • I still get 1e6 with plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0), useMathText=True) –  Jan 24 '19 at 20:50
  • @user47014 Which matplotlib version are you using? – ImportanceOfBeingErnest Jan 24 '19 at 20:54
  • matplotlib.__version__ shows 2.0.2 –  Jan 24 '19 at 20:56
  • I will try updating –  Jan 24 '19 at 20:57
  • Nevermind, I gave an answer including the case of 2.0.2 – ImportanceOfBeingErnest Jan 24 '19 at 21:08

1 Answers1

2

The offset is formatted differently depending on the useMathText argument. If True it will show the offset in a latex-like (MathText) format as x 10^6 instead of 1e6

import numpy as np
import matplotlib.pyplot as plt
plt.figure()
x = np.linspace(0,1000)
y = x**2
plt.plot(x,y)
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0), useMathText=True)
plt.show()

enter image description here

Note that the above will not work for version 2.0.2 (possibly other older versions). In that case you need to set the formatter manually and specify the option:

import numpy as np
import matplotlib.pyplot as plt
plt.figure()
x = np.linspace(0,1000)
y = x**2
plt.plot(x,y)
plt.gca().yaxis.set_major_formatter(plt.ScalarFormatter(useMathText=True))
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
plt.show()
ImportanceOfBeingErnest
  • 251,038
  • 37
  • 461
  • 518