I would like to customize the offset use in matplotlib. Specifically:
- Set the limits when used (similar to power limits for scientific notation)
- Customize the format string on both the axis ticks. Doing so with
ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('%.1f'))switches the scientific notation off.
Thank you for the answer, in case somebody encounters the same problem, here's the code I used to solve it:
import matplotlib.pyplot as plt
from matplotlib import ticker
import numpy as np
class ScalarFormatter(ticker.ScalarFormatter):
def __init__(self, useOffset=None, useMathText=None, useLocale=None):
ticker.ScalarFormatter.__init__(self, useOffset, useMathText, useLocale)
self._powerlimits = (0, 0)
def _set_offset(self, range):
mean_locs = np.mean(self.locs)
if range / 2 < np.absolute(mean_locs):
ave_oom = np.floor(np.log10(mean_locs))
p10 = 10 ** np.floor(np.log10(range))
self.offset = (np.ceil(np.mean(self.locs) / p10) * p10)
else:
self.offset = 0
def get_offset(self, txt=''):
if self.orderOfMagnitude:
txt += u'\u00D7' + '1e%d' % self.orderOfMagnitude + ' '
if self.offset:
if self.offset > 0:
txt += '+'
txt += self.format_data(self.offset)
return self.fix_minus(txt)