3 """A number formatting utilities for Hooke.
6 from matplotlib.ticker import ScalarFormatter
8 from . import curve as lhc
11 class EngrFormatter(ScalarFormatter):
12 """A variation of the standard ScalarFormatter, using only multiples of
14 in the mantissa. A fixed number of decimals can be displayed with the optional
15 parameter `ndec` . If `ndec` is None (default), the number of decimals is
17 from the current ticks.
19 def __init__(self, ndec=None, useOffset=True, useMathText=False):
20 ScalarFormatter.__init__(self, useOffset, useMathText)
21 if ndec is None or ndec < 0:
26 self.format = "%%1.%if" % ndec
28 def _set_orderOfMagnitude(self, mrange):
29 """Sets the order of magnitude."""
30 locs = numpy.absolute(self.locs)
32 oom = numpy.floor(numpy.log10(mrange))
34 if locs[0] > locs[-1]:
41 oom = numpy.floor(numpy.log10(val))
43 self.orderOfMagnitude = 3*(oom//3)
45 self.orderOfMagnitude = -3
47 self.orderOfMagnitude = 3*(oom//3)
49 self.orderOfMagnitude = 0
51 def _set_format(self):
52 """Sets the format string to format all ticklabels."""
53 # set the format string to format all the ticklabels
54 locs = (numpy.array(self.locs)-self.offset) / 10**self.orderOfMagnitude+1e-15
55 sigfigs = [len(str('%1.3f'% loc).split('.')[1].rstrip('0')) \
58 if self.format is None:
59 self.format = '%1.' + str(sigfigs[-1]) + 'f'
60 if self._usetex or self._useMathText: self.format = '$%s$'%self.format