2 # -*- coding: utf-8 -*-
7 Number formatting utilities for Hooke.
9 Copyright (C) 2006 Massimo Sandal (University of Bologna, Italy).
10 With algorithms contributed by Francesco Musiani (University of Bologna, Italy)
12 This program is released under the GNU General Public License version 2.
15 from matplotlib.ticker import ScalarFormatter
17 from . import curve as lhc
20 class EngrFormatter(ScalarFormatter):
21 """A variation of the standard ScalarFormatter, using only multiples of
23 in the mantissa. A fixed number of decimals can be displayed with the optional
24 parameter `ndec` . If `ndec` is None (default), the number of decimals is
26 from the current ticks.
28 def __init__(self, ndec=None, useOffset=True, useMathText=False):
29 ScalarFormatter.__init__(self, useOffset, useMathText)
30 if ndec is None or ndec < 0:
35 self.format = "%%1.%if" % ndec
37 def _set_orderOfMagnitude(self, mrange):
38 """Sets the order of magnitude."""
39 locs = numpy.absolute(self.locs)
41 oom = numpy.floor(numpy.log10(mrange))
43 if locs[0] > locs[-1]:
50 oom = numpy.floor(numpy.log10(val))
52 self.orderOfMagnitude = 3*(oom//3)
54 self.orderOfMagnitude = -3
56 self.orderOfMagnitude = 3*(oom//3)
58 self.orderOfMagnitude = 0
60 def _set_format(self):
61 """Sets the format string to format all ticklabels."""
62 # set the format string to format all the ticklabels
63 locs = (numpy.array(self.locs)-self.offset) / 10**self.orderOfMagnitude+1e-15
64 sigfigs = [len(str('%1.3f'% loc).split('.')[1].rstrip('0')) \
67 if self.format is None:
68 self.format = '%1.' + str(sigfigs[-1]) + 'f'
69 if self._usetex or self._useMathText: self.format = '$%s$'%self.format