1 # Copyright (C) 2006-2010 Francesco Musiani
2 # Massimo Sandal <devicerandom@gmail.com>
3 # W. Trevor King <wking@drexel.edu>
5 # This file is part of Hooke.
7 # Hooke is free software: you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation, either
10 # version 3 of the License, or (at your option) any later version.
12 # Hooke is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with Hooke. If not, see
19 # <http://www.gnu.org/licenses/>.
21 """A number formatting utilities for Hooke.
24 from matplotlib.ticker import ScalarFormatter
26 from . import curve as lhc
29 class EngrFormatter(ScalarFormatter):
30 """A variation of the standard ScalarFormatter, using only multiples of
32 in the mantissa. A fixed number of decimals can be displayed with the optional
33 parameter `ndec` . If `ndec` is None (default), the number of decimals is
35 from the current ticks.
37 def __init__(self, ndec=None, useOffset=True, useMathText=False):
38 ScalarFormatter.__init__(self, useOffset, useMathText)
39 if ndec is None or ndec < 0:
44 self.format = "%%1.%if" % ndec
46 def _set_orderOfMagnitude(self, mrange):
47 """Sets the order of magnitude."""
48 locs = numpy.absolute(self.locs)
50 oom = numpy.floor(numpy.log10(mrange))
52 if locs[0] > locs[-1]:
59 oom = numpy.floor(numpy.log10(val))
61 self.orderOfMagnitude = 3*(oom//3)
63 self.orderOfMagnitude = -3
65 self.orderOfMagnitude = 3*(oom//3)
67 self.orderOfMagnitude = 0
69 def _set_format(self):
70 """Sets the format string to format all ticklabels."""
71 # set the format string to format all the ticklabels
72 locs = (numpy.array(self.locs)-self.offset) / 10**self.orderOfMagnitude+1e-15
73 sigfigs = [len(str('%1.3f'% loc).split('.')[1].rstrip('0')) \
76 if self.format is None:
77 self.format = '%1.' + str(sigfigs[-1]) + 'f'
78 if self._usetex or self._useMathText: self.format = '$%s$'%self.format