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.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é

Depending on the range of your values, “%g” might do want you want. E.g.:

···

On Fri, Aug 19, 2011 at 3:12 AM, André Dankert <andre.dankert@…982…> 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)

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(2pit)

plot(t,s)

ax = gca()

ax.xaxis.set_major_formatter(majorFormatter)

show()


Warren

  1. 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

From: André Dankert [mailto:andre.dankert@…3743…]
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.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.

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.

rstrip_formatter.py (1.82 KB)