removing leading zeros in ticklabels

hi all,

when i make any numeric scatter plot containing floats, the formatted tick labels always have leading zeros, e.g “0.5” as opposed to “.5” in the labels.

for example:

x = rand(10)
scatter(x,x)

is there any way to change this to remove the leading zeros? i have tried:

s = subplot(111)
majorFormatter = FormatStrFormatter(’%0.1f’)
s.xaxis.set_major_formatter(majorFormatter)
scatter(x,x)

but it does not work. i also tried “%.f” but it does not work either. the matlab default is to plot without the leading zero and i am trying to recreate this.

thank you.

FormatStrFormatter (and other formatters) rely on Python's string
interpolation, and It does not seem to be possible to get rid of the
leading zero (Built-in Types — Python 3.12.0 documentation).

I think what you can do is to replace "0." with "." after the
interpolation. Something like below works for me.

from matplotlib.ticker import ScalarFormatter
s = subplot(111)

class ScalarFormatterNoLeadingZero(ScalarFormatter):
    def pprint_val(self, x):
        s = ScalarFormatter.pprint_val(self, x)
        return s.replace("0.",".")

s.xaxis.set_major_formatter(ScalarFormatterNoLeadingZero())

HTH,

-JJ

···

On Sat, Feb 28, 2009 at 4:52 PM, per freem <perfreem@...287...> wrote:

hi all,

when i make any numeric scatter plot containing floats, the formatted tick
labels always have leading zeros, e.g "0.5" as opposed to ".5" in the
labels.

for example:

x = rand(10)
scatter(x,x)

is there any way to change this to remove the leading zeros? i have tried:

s = subplot(111)
majorFormatter = FormatStrFormatter('%0.1f')
s.xaxis.set_major_formatter(majorFormatter)
scatter(x,x)

but it does not work. i also tried "%.f" but it does not work either. the
matlab default is to plot without the leading zero and i am trying to
recreate this.

thank you.

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options