As far as I can tell, matplotlib does not offer exactly this options out of the box. The documentation is indeed sparse (Ticker API is the place to go). The Formatter classes are responsible for formatting the tick values. Out of the ones offered, only ScalarFormatter (the default formatter) offers scientific formatting, however, it does not allow the exponent or number of significant digits to be fixed. One alternative would be to use either FixedFormatter or FuncFormatter, which essentially allow you to freely choose the tick values (the former can be indirectly selected using
plt.gca().set_xticklabels). However, none of them allow you to choose the so called offset_string which is the string displayed at the end of the axis, customary used for a value offset, but ScalarFormatter also uses it for the scientific multiplier.
Thus, my best solution consists of a custom formatter derived from ScalarFormatter, where instead of autodetecting order of magnitude and format string, those are just fixed by the used:
from matplotlib import rcParams
import matplotlib.ticker
if 'axes.formatter.useoffset' in rcParams:
# None triggers use of the rcParams value
useoffsetdefault = None
else:
# None would raise an exception
useoffsetdefault = True
class FixedScalarFormatter(matplotlib.ticker.ScalarFormatter):
def __init__(self, format, orderOfMagnitude=0, useOffset=useoffsetdefault, useMathText=None, useLocale=None):
super(FixedScalarFormatter,self).__init__(useOffset=useOffset,useMathText=useMathText,useLocale=useLocale)
self.base_format = format
self.orderOfMagnitude = orderOfMagnitude
def _set_orderOfMagnitude(self, range):
""" Set orderOfMagnitude to best describe the specified data range.
Does nothing except from preventing the parent class to do something.
"""
pass
def _set_format(self, vmin, vmax):
""" Calculates the most appropriate format string for the range (vmin, vmax).
We're actually just using a fixed format string.
"""
self.format = self.base_format
if self._usetex:
self.format = '$%s$' % self.format
elif self._useMathText:
self.format = '$\mathdefault{%s}$' % self.format
Note that the default value of ScalarFormatter's constructor parameter useOffset changed at some point, mine tries to guess which one is the right one.
Attach this class to one or both axes of your plots as follows:
plt.gca().xaxis.set_major_formatter(FixedScalarFormatter('%.2f',4))
plt.gca().yaxis.set_major_formatter(FixedScalarFormatter('%.2f',4))