From 952b9c888a62643b8c7454bdedb493ff292d26f3 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 31 Jul 2010 16:10:03 -0400 Subject: [PATCH] Moved HookeScalarFormatter from formatter -> panel.plot. --- hooke/ui/gui/formatter.py | 78 -------------------------------------- hooke/ui/gui/panel/plot.py | 42 +++++++++++++++++++- 2 files changed, 40 insertions(+), 80 deletions(-) delete mode 100644 hooke/ui/gui/formatter.py diff --git a/hooke/ui/gui/formatter.py b/hooke/ui/gui/formatter.py deleted file mode 100644 index 66fbbe2..0000000 --- a/hooke/ui/gui/formatter.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright (C) 2006-2010 Francesco Musiani -# Massimo Sandal -# W. Trevor King -# -# This file is part of Hooke. -# -# Hooke is free software: you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation, either -# version 3 of the License, or (at your option) any later version. -# -# Hooke is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with Hooke. If not, see -# . - -"""A number formatting utilities for Hooke. -""" - -from matplotlib.ticker import ScalarFormatter - -from . import curve as lhc - - -class EngrFormatter(ScalarFormatter): - """A variation of the standard ScalarFormatter, using only multiples of -three -in the mantissa. A fixed number of decimals can be displayed with the optional -parameter `ndec` . If `ndec` is None (default), the number of decimals is -defined -from the current ticks. - """ - def __init__(self, ndec=None, useOffset=True, useMathText=False): - ScalarFormatter.__init__(self, useOffset, useMathText) - if ndec is None or ndec < 0: - self.format = None - elif ndec == 0: - self.format = "%d" - else: - self.format = "%%1.%if" % ndec - - def _set_orderOfMagnitude(self, mrange): - """Sets the order of magnitude.""" - locs = numpy.absolute(self.locs) - if self.offset: - oom = numpy.floor(numpy.log10(mrange)) - else: - if locs[0] > locs[-1]: - val = locs[0] - else: - val = locs[-1] - if val == 0: - oom = 0 - else: - oom = numpy.floor(numpy.log10(val)) - if oom <= -3: - self.orderOfMagnitude = 3*(oom//3) - elif oom <= -1: - self.orderOfMagnitude = -3 - elif oom >= 4: - self.orderOfMagnitude = 3*(oom//3) - else: - self.orderOfMagnitude = 0 - - def _set_format(self): - """Sets the format string to format all ticklabels.""" - # set the format string to format all the ticklabels - locs = (numpy.array(self.locs)-self.offset) / 10**self.orderOfMagnitude+1e-15 - sigfigs = [len(str('%1.3f'% loc).split('.')[1].rstrip('0')) \ - for loc in locs] - sigfigs.sort() - if self.format is None: - self.format = '%1.' + str(sigfigs[-1]) + 'f' - if self._usetex or self._useMathText: self.format = '$%s$'%self.format diff --git a/hooke/ui/gui/panel/plot.py b/hooke/ui/gui/panel/plot.py index d1d5a9d..e46c819 100644 --- a/hooke/ui/gui/panel/plot.py +++ b/hooke/ui/gui/panel/plot.py @@ -15,7 +15,7 @@ matplotlib.use('WXAgg') # use wxpython with antigrain (agg) rendering from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas from matplotlib.backends.backend_wx import NavigationToolbar2Wx as NavToolbar from matplotlib.figure import Figure -from matplotlib.ticker import Formatter +from matplotlib.ticker import Formatter, ScalarFormatter import wx from ....util.callback import callback, in_callback @@ -38,6 +38,44 @@ class HookeFormatter (Formatter): return ppSI(value=x, unit=self.unit, decimals=self.decimals) +class HookeScalarFormatter (ScalarFormatter): + """:class:`matplotlib.ticker.ScalarFormatter` using only multiples + of three in the mantissa. + + A fixed number of decimals can be displayed with the optional + parameter `decimals` . If `decimals` is `None` (default), the number + of decimals is defined from the current ticks. + """ + def __init__(self, decimals=None, **kwargs): + # Can't use super() because ScalarFormatter is an old-style class :(. + ScalarFormatter.__init__(self, **kwargs) + self._decimals = decimals + + def _set_orderOfMagnitude(self, *args, **kwargs): + """Sets the order of magnitude.""" + # Can't use super() because ScalarFormatter is an old-style class :(. + ScalarFormatter._set_orderOfMagnitude(self, *args, **kwargs) + self.orderOfMagnitude -= self.orderOfMagnitude % 3 + + def _set_format(self, *args, **kwargs): + """Sets the format string to format all ticklabels.""" + # Can't use super() because ScalarFormatter is an old-style class :(. + ScalarFormatter._set_format(self, *args, **kwargs) + if self._decimals is None or self._decimals < 0: + locs = (np.asarray(self.locs)-self.offset) / 10**self.orderOfMagnitude+1e-15 + sigfigs = [len(str('%1.8f'% loc).split('.')[1].rstrip('0')) \ + for loc in locs] + sigfigs.sort() + decimals = sigfigs[-1] + else: + decimals = self._decimals + self.format = '%1.' + str(decimals) + 'f' + if self._usetex: + self.format = '$%s$' % self.format + elif self._useMathText: + self.format = '$\mathdefault{%s}$' % self.format + + class PlotPanel (Panel, wx.Panel): """UI for graphical curve display. """ @@ -163,7 +201,7 @@ class PlotPanel (Panel, wx.Panel): d = int(config['plot decimals']) # TODO: config should convert x_n, x_unit = split_data_label(x_name) y_n, y_unit = split_data_label(y_name) - fx = HookeFormatter(decimals=d, unit=x_unit) + fx = HookeFormatter(decimals=d)#, unit=x_unit) axes.xaxis.set_major_formatter(fx) fy = HookeFormatter(decimals=d, unit=y_unit) axes.yaxis.set_major_formatter(fy) -- 2.26.2