Re: [Matplotlib-users] Define minimum precision for tick labels
From: André Dankert [mailto:andre.dank...@googlemail.com] Sent: Friday, August 19, 2011 04:12 I have a minor style problem, but, nevertheless, I can't solve it with googles help. I want to have a minimum precision displayed on my ticks, i.e. if the ticks are -1,-0.5,0,0.5,1 it should be displayed this way instead of -1.0,-0.5,0.0,0.5,1.0. With the tick formatter I can set the tick precission, but only for the whole axis (if I set %d for the example, it will be -1,0,0,0,1). Also I use MaxNLocator, so I don't predefine the ticks (I thought it's more convinient to use this already done procedure instead of writing my own), so I'm more flexible, for example when I zoom in, but therefore I don't even know, which the maximum precision is. Possible solutions would be: 1. There is a number precision definition (something like %x.xf) which produces numbers with minimum precision (again -1. becomes -1 and 2.47300 becomes 2.473) 2. Extract by MaxNLocator defined ticks, edit them accordingly and reassign them (which would be still a lot of work) I even checked for solutions in Latex (because I use Latex string coding), but this is even more inflexible, when it comes to numbers. But maybe someone knows anything. I took a third approach in the attachment, deriving a new formatter class from ScalarFormatter. That's more complicated, but it preserves the offset and other features of ScalarFormatter. It works for me with or without MathText enabled; I haven't tested it with usetex on. Note that I overrode a private method, so future breakage may be more likely than it would be otherwise. I hope it helps. import numpy as np import matplotlib.ticker as mticker class RStripScalarFormatter(mticker.ScalarFormatter): """ Formats as ScalarFormatter but without trailing zeros. The 'format' attribute of ScalarFormatter instances contains the basic number formatting as well as the formatting for TeX or MathText. Here we separate those into the 'format' and 'wrapformat' attributes, respectively. """ # Code adapted from mticker.ScalarFormatter. def __init__(self, useOffset=True, useMathText=False): mticker.ScalarFormatter.__init__( self, useOffset=useOffset, useMathText=useMathText ) if self._usetex: self.wrapformat = '$%s$' elif self._useMathText: self.wrapformat = '$\mathdefault{%s}$' else: self.wrapformat = '%s' def _set_format(self): # set the format string to format all the ticklabels # The floating point black magic (adding 1e-15 and formatting # to 8 digits) may warrant review and cleanup. locs = ( (np.asarray(self.locs) - self.offset) / 10**self.orderOfMagnitude ) + 1e-15 maxsigfigs = max( len(str('%1.8f' % loc).split('.')[1].rstrip('0')) for loc in locs ) self.format = '%1.' + str(maxsigfigs) + 'f' def pprint_val(self, x): xp = (x - self.offset) / 10**self.orderOfMagnitude if np.absolute(xp) < 1e-8: xp = 0 xpstr = ( (self.format % xp).rstrip('0').rstrip('.') or '0' ) # If nothing is left, it must've been zero. return self.wrapformat % xpstr if __name__ == '__main__': import matplotlib.pyplot as plt plt.plot([-0.005, 0.02]) axes = plt.gca() axes.yaxis.set_major_formatter(RStripScalarFormatter()) plt.show() -- uberSVN's rich system and user administration capabilities and model configuration take the hassle out of deploying and managing Subversion and the tools developers use with it. Learn more about uberSVN and get a free download at: http://p.sf.net/sfu/wandisco-dev2dev ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Define minimum precision for tick labels
On Fri, Aug 19, 2011 at 3:12 AM, André Dankert wrote: > Hi, > > I have a minor style problem, but, nevertheless, I can't solve it with > googles help. I want to have a minimum precision displayed on my ticks, i.e. > if the ticks are -1,-0.5,0,0.5,1 it should be displayed this way instead of > -1.0,-0.5,0.0,0.5,1.0. With the tick formatter I can set the tick > precission, but only for the whole axis (if I set %d for the example, it > will be -1,0,0,0,1). Also I use MaxNLocator, so I don't predefine the ticks > (I thought it's more convinient to use this already done procedure instead > of writing my own), so I'm more flexible, for example when I zoom in, but > therefore I don't even know, which the maximum precision is. > > Possible solutions would be: > 1. There is a number precision definition (something like %x.xf) which > produces numbers with minimum precision (again -1. becomes -1 and > 2.47300 becomes 2.473) > Depending on the range of your values, "%g" might do want you want. E.g.: - from numpy import linspace, sin, pi from matplotlib.pyplot import plot, show, gca from matplotlib.ticker import FormatStrFormatter majorFormatter = FormatStrFormatter('%g') t = linspace(-1.0, 1.0, 41) s = sin(2*pi*t) plot(t,s) ax = gca() ax.xaxis.set_major_formatter(majorFormatter) show() - Warren > 2. Extract by MaxNLocator defined ticks, edit them accordingly and reassign > them (which would be still a lot of work) > > I even checked for solutions in Latex (because I use Latex string coding), > but this is even more inflexible, when it comes to numbers. But maybe > someone knows anything. > > Thanks! > > Best regards, > André > > > -- > Get a FREE DOWNLOAD! and learn more about uberSVN rich system, > user administration capabilities and model configuration. Take > the hassle out of deploying and managing Subversion and the > tools developers use with it. http://p.sf.net/sfu/wandisco-d2d-2 > ___ > Matplotlib-users mailing list > Matplotlib-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > -- Get a FREE DOWNLOAD! and learn more about uberSVN rich system, user administration capabilities and model configuration. Take the hassle out of deploying and managing Subversion and the tools developers use with it. http://p.sf.net/sfu/wandisco-d2d-2___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
[Matplotlib-users] Define minimum precision for tick labels
Hi, I have a minor style problem, but, nevertheless, I can't solve it with googles help. I want to have a minimum precision displayed on my ticks, i.e. if the ticks are -1,-0.5,0,0.5,1 it should be displayed this way instead of -1.0,-0.5,0.0,0.5,1.0. With the tick formatter I can set the tick precission, but only for the whole axis (if I set %d for the example, it will be -1,0,0,0,1). Also I use MaxNLocator, so I don't predefine the ticks (I thought it's more convinient to use this already done procedure instead of writing my own), so I'm more flexible, for example when I zoom in, but therefore I don't even know, which the maximum precision is. Possible solutions would be: 1. There is a number precision definition (something like %x.xf) which produces numbers with minimum precision (again -1. becomes -1 and 2.47300 becomes 2.473) 2. Extract by MaxNLocator defined ticks, edit them accordingly and reassign them (which would be still a lot of work) I even checked for solutions in Latex (because I use Latex string coding), but this is even more inflexible, when it comes to numbers. But maybe someone knows anything. Thanks! Best regards, André -- Get a FREE DOWNLOAD! and learn more about uberSVN rich system, user administration capabilities and model configuration. Take the hassle out of deploying and managing Subversion and the tools developers use with it. http://p.sf.net/sfu/wandisco-d2d-2___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users