109

I created this plot using Matlab

enter image description here

Using matplotlib, the x-axies draws large numbers such as 100000, 200000, 300000. I would like to have something like 1, 2, 3 and a 10^5 to indicate that it's actually 100000, 200000, 300000.

Is there a simple way to create such scale in matplotlib?

4 Answers 4

168

Try using matplotlib.pyplot.ticklabel_format:

import matplotlib.pyplot as plt
...
plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))

This applies scientific notation (i.e. a x 10^b) to your x-axis tickmarks

8
  • 10
    is it possible to add an offset? (e.g. instead of 0.0, 0.5, 1.0, 1.5, ... and x 10^b it will be 0, 5, 10, 15,... and x 10^{b-1})
    – Eagle
    Jul 20, 2012 at 13:20
  • 26
    ticklabel_format is also a method for the axes objects, you can try something like ax.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
    – Covich
    Mar 22, 2016 at 10:47
  • 3
    @Eagle sorry, your follow-up question about adding an offset was what I was referring to. my x-axis is [0.1, 0.2, ...] and I'd prefer [1, 2, ...] with an adjusted exponent. were you able to find a solution for that?
    – trianta2
    Jul 18, 2017 at 16:29
  • 1
    Hint: use option UseMathText=True to get math notation instead of 1e6
    – Quastiat
    Oct 8, 2019 at 7:19
  • 1
    Hint full version: plt.gca().ticklabel_format(useMathText=True) and magnitude will be displayed as the number 10
    – Plo_Koon
    Apr 15, 2020 at 8:10
19

This is not so much an answer to your original question as to one of the queries you had in the body of your question.

A little preamble, so that my naming doesn't seem strange:

import matplotlib
from matplotlib import rc
from matplotlib.figure import Figure
ax = self.figure.add_subplot( 111 )

As has been mentioned you can use ticklabel_format to specify that matplotlib should use scientific notation for large or small values:

ax.ticklabel_format(style='sci',scilimits=(-3,4),axis='both')

You can affect the way that this is displayed using the flags in rcParams (from matplotlib import rcParams) or by setting them directly. I haven't found a more elegant way of changing between '1e' and 'x10^' scientific notation than:

ax.xaxis.major.formatter._useMathText = True

This should give you the more Matlab-esc, and indeed arguably better appearance. I think the following should do the same:

rc('text', usetex=True)
0
12

The scalar formatter supports collecting the exponents. The docs are as follows:

class matplotlib.ticker.ScalarFormatter(useOffset=True, useMathText=False, useLocale=None) Bases: matplotlib.ticker.Formatter

Tick location is a plain old number. If useOffset==True and the data range is much smaller than the data average, then an offset will be determined such that the tick labels are meaningful. Scientific notation is used for data < 10^-n or data >= 10^m, where n and m are the power limits set using set_powerlimits((n,m)). The defaults for these are controlled by the axes.formatter.limits rc parameter.

your technique would be:

from matplotlib.ticker import ScalarFormatter
xfmt = ScalarFormatter()
xfmt.set_powerlimits((-3,3))  # Or whatever your limits are . . .
{{ Make your plot }}
gca().xaxis.set_major_formatter(xfmt)

To get the exponent displayed in the format x10^5, instantiate the ScalarFormatter with useMathText=True.

After Image

You could also use:

xfmt.set_useOffset(10000)

To get a result like this:

enter image description here

1
  • 3
    Instantiate the ScalarFormatter with useMathText=True to get x10^4. Mar 6, 2015 at 16:39
8

I find the simple solution

pylab.ticklabel_format(axis='y',style='sci',scilimits=(1,4))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.