## matplotlib-users

 [Matplotlib-users] Define minimum precision for tick labels From: André Dankert - 2011-08-19 08:12:40 Attachments: Message as HTML ```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.0000 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é ```
 Re: [Matplotlib-users] Define minimum precision for tick labels From: Warren Weckesser - 2011-08-19 15:52:39 Attachments: Message as HTML ```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.0000 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@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > ```
 Re: [Matplotlib-users] Define minimum precision for tick labels From: Stan West - 2011-08-22 21:06:21 Attachments: text/plain ```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() ```