FuncFormatter and Colorbar

Hi to all!

I have a question concerning reformatting of axis-ticks via the FuncFormatter-class. In german, it’s common to use a comma as separator for decimal numbers instead of a dot. To realize it in matplotlib, I do something like

from matplotlib.ticker import FuncFormatter
import pylab
pylab.figure()
formatter = FuncFormatter(lambda x,pos: ("%.2f"%x).replace(".",","))
ax = pylab.axes()
ax.xaxis.set_major_formatter
(formatter)
ax.yaxis.set_major_formatter(formatter)
ax.plot(pylab.arange(0,1,0.1),pylab.arange(0,1,0.1))

This works fine for me, but I encounter a problem when I do an imshow-command with a colorbar. In the imshow-axes, it’s o.k., but for the colorbar it doesn’t really work. I do

cb = pylab.colorbar()
cb.ax.yaxis.set_major_formatter(formatter)

and, actually, all dots are replaced by com9mata, but the values are also changed! E.g. instead of the old values (without formatter) from 0-0.54, the values are increased to 0-0.95. The complete code for this example would be:

from matplotlib.ticker import FuncFormatter, ScalarFormatter
import pylab

pylab.figure()
formatter = FuncFormatter(lambda x,pos: ("%.2f"%x).replace(".",","))
ax = pylab.axes()
ax.xaxis.set_major_formatter(formatter)
ax.yaxis.set_major_formatter(formatter)

data = pylab.zeros((100,100),“d”)
data[10,10] = 0.567
pylab.imshow(data)
cb = pylab.colorbar()
cb.ax.yaxis.set_major_formatter(formatter)

Can anyone explain why it doesn’t work out as I expect it to work? Or is there a better, more standard way to substitute the dots by commata?

Thanks,
Thorsten

···

Hello list,
Hello Thorsten,

I have a question concerning reformatting of axis-ticks via the
FuncFormatter-class. In german, it's common to use a comma as separator for
decimal numbers instead of a dot. To realize it in matplotlib, I do
something like

>from matplotlib.ticker import FuncFormatter

import pylab
pylab.figure()
formatter = FuncFormatter(lambda x,pos: ("%.2f"%x).replace(".",","))
ax = pylab.axes()
ax.xaxis.set_major_formatter(formatter)
ax.yaxis.set_major_formatter(formatter)
ax.plot(pylab.arange(0,1,0.1),pylab.arange(0,1,0.1))
This works fine for me,

I had the same idea ;-). The problem is that you have a fixed number of digits
behind the comma, which is not the desirable behaviour during zoom.
I changed the ticker.py/ axes.py files to circumwait this disadvantage. I
attached a patch showing my changes and maybe somebody can test it.
You can activate it using:
  ax.ticklabel_format(style='comma')
for an ScalarFormatter

but I encounter a problem when I do an
imshow-command with a colorbar. In the imshow-axes, it's o.k., but for the
colorbar it doesn't really work. I do

cb = pylab.colorbar()
cb.ax.yaxis.set_major_formatter(formatter)

and, actually, all dots are replaced by com9mata, but the values are also
changed! E.g. instead of the old values (without formatter) from 0-0.54,
the

values are increased to 0-0.95.

[...]

Can anyone explain why it doesn't work out as I expect it to work?

I don't know were the problem comes from. I attached your example in a slitly
modified version and this shows that the problem is not due to your special
formatting. It occurs with matplotlib.ticker.ScalarFormatter, too.

best regards,
Matthias

comma.patch (3.83 KB)

example_of_thorsten_kranz.py (591 Bytes)

···

On Wednesday 09 January 2008 11:38, Thorsten Kranz wrote:

Or is there a better, more standard way to substitute the dots by commata?

Thanks,
Thorsten

Hi list, Hi Matthias,

I found another way to deal with this problem. when defining the colorbar, one can give an additional kwarg “format”, so by defining the kwarg “format=formatter”, we solved the problem.

Anyway, I think an option as Matthias implemented would be very handy for all those users like us here in Germany who might want to have the numbers formatted with commata.

Greetings,
Thorsten

2008/1/9, Matthias Michler <MatthiasMichler@…361…>:

···

Hello list,
Hello Thorsten,

On Wednesday 09 January 2008 11:38, Thorsten Kranz wrote:

I have a question concerning reformatting of axis-ticks via the
FuncFormatter-class. In german, it’s common to use a comma as separator for

decimal numbers instead of a dot. To realize it in matplotlib, I do
something like

from matplotlib.ticker import FuncFormatter

import pylab
pylab.figure()
formatter = FuncFormatter(lambda x,pos: ("%.2f"%x).replace(".",","))

ax = pylab.axes()
ax.xaxis.set_major_formatter(formatter)
ax.yaxis.set_major_formatter(formatter)
ax.plot(pylab.arange(0,1,0.1),pylab.arange(0,1,0.1))
This works fine for me,

I had the same idea ;-). The problem is that you have a fixed number of digits
behind the comma, which is not the desirable behaviour during zoom.
I changed the ticker.py/ axes.py files to circumwait this disadvantage. I

attached a patch showing my changes and maybe somebody can test it.
You can activate it using:
ax.ticklabel_format(style=‘comma’)
for an ScalarFormatter

but I encounter a problem when I do an

imshow-command with a colorbar. In the imshow-axes, it’s o.k., but for the
colorbar it doesn’t really work. I do

cb = pylab.colorbar()
cb.ax.yaxis.set_major_formatter(formatter)

and, actually, all dots are replaced by com9mata, but the values are also
changed! E.g. instead of the old values (without formatter) from 0-0.54,
the

values are increased to 0-0.95.
[…]
Can anyone explain why it doesn’t work out as I expect it to work?
I don’t know were the problem comes from. I attached your example in a slitly
modified version and this shows that the problem is not due to your special

formatting. It occurs with matplotlib.ticker.ScalarFormatter, too.

best regards,
Matthias

Or is there a better, more standard way to substitute the dots by commata?

Thanks,
Thorsten


Check out the new SourceForge.net Marketplace.
It’s the best place to buy or sell services for
just about anything Open Source.

http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace


Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Thanks for the patch. However, perhaps a more general solution would be to use the Python locale module to format numbers according to different locales. And expose a kwarg select between the user's preferred locale, the current U.S. English-centric defaults as they are now, or an arbitrary locale using an ISO language code. That seems like it could be a better long-term solution, since there are different number formats all over, not just in Germany.

All that said, internationalization is hard -- especially for us sheltered people in the U.S. where the defaults are most often correct. I may be missing an important detail here.

Cheers,
Mike

Thorsten Kranz wrote:

···

Hi list, Hi Matthias,

I found another way to deal with this problem. when defining the colorbar, one can give an additional kwarg "format", so by defining the kwarg "format=formatter", we solved the problem.

Anyway, I think an option as Matthias implemented would be very handy for all those users like us here in Germany who might want to have the numbers formatted with commata.

Greetings,
Thorsten

2008/1/9, Matthias Michler <MatthiasMichler@…361… <mailto:MatthiasMichler@…361…>>:

    Hello list,
    Hello Thorsten,

    On Wednesday 09 January 2008 11:38, Thorsten Kranz wrote:
     > I have a question concerning reformatting of axis-ticks via the
     > FuncFormatter-class. In german, it's common to use a comma as
    separator for
     > decimal numbers instead of a dot. To realize it in matplotlib, I do
     > something like
     >
     > >from matplotlib.ticker import FuncFormatter
     >
     > import pylab
     > pylab.figure()
     > formatter = FuncFormatter(lambda x,pos: ("%.2f"%x).replace(".",","))
     > ax = pylab.axes()
     > ax.xaxis.set_major_formatter(formatter)
     > ax.yaxis.set_major_formatter(formatter)
     > ax.plot(pylab.arange(0,1,0.1),pylab.arange(0,1,0.1))
     > This works fine for me,

    I had the same idea ;-). The problem is that you have a fixed number
    of digits
    behind the comma, which is not the desirable behaviour during zoom.
    I changed the ticker.py/ axes.py files to circumwait this
    disadvantage. I
    attached a patch showing my changes and maybe somebody can test it.
    You can activate it using:
      ax.ticklabel_format(style='comma')
    for an ScalarFormatter

     > but I encounter a problem when I do an
     > imshow-command with a colorbar. In the imshow-axes, it's o.k.,
    but for the
     > colorbar it doesn't really work. I do
     >
     > cb = pylab.colorbar()
     > cb.ax.yaxis.set_major_formatter(formatter)
     >
     > and, actually, all dots are replaced by com9mata, but the values
    are also
     > changed! E.g. instead of the old values (without formatter) from
    0-0.54,
     > the
     >
     > values are increased to 0-0.95.
    [...]
     > Can anyone explain why it doesn't work out as I expect it to work?
    I don't know were the problem comes from. I attached your example in
    a slitly
    modified version and this shows that the problem is not due to your
    special
    formatting. It occurs with matplotlib.ticker.ScalarFormatter, too.

    best regards,
    Matthias

     > Or is there a better, more standard way to substitute the dots by
    commata?
     >
     > Thanks,
     > Thorsten

    -------------------------------------------------------------------------
    Check out the new SourceForge.net Marketplace.
    It's the best place to buy or sell services for
    just about anything Open Source.
    http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
    _______________________________________________
    Matplotlib-users mailing list
    Matplotlib-users@lists.sourceforge.net
    <mailto:Matplotlib-users@lists.sourceforge.net>
    https://lists.sourceforge.net/lists/listinfo/matplotlib-users
    <https://lists.sourceforge.net/lists/listinfo/matplotlib-users>

------------------------------------------------------------------------

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace

------------------------------------------------------------------------

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA

Michael Droettboom wrote:

Thanks for the patch. However, perhaps a more general solution would be to use the Python locale module to format numbers according to different locales. And expose a kwarg select between the user's preferred locale, the current U.S. English-centric defaults as they are now, or an arbitrary locale using an ISO language code. That seems like it could be a better long-term solution, since there are different number formats all over, not just in Germany.

All that said, internationalization is hard -- especially for us sheltered people in the U.S. where the defaults are most often correct. I may be missing an important detail here.

Cheers,
Mike

Mike,

I agree that this problem needs to be solved. I was looking at it a year or so ago, with the idea of putting in simple options rather than full internationalization, but I never followed up on it. Until your message I had never looked at the documentation for the locale module. It *might* be possible to fix the formatters by replacing a few "somestring % variables" constructs with calls to locale.format_string(somestring, variables)--but this is new in python 2.5. Without this it looks like it might be much harder. The formatter code is already a bit convoluted because of all the variations--latex, mathtex, plain, with or without scientific notation.

Actually, I think the option I was looking at a year ago was what is handled by the "grouping" arg in locale.format_string--the ability to use commas or dots to break up triplets of digits.

Eric

Hello Mike,
Hello list,

Thanks for the patch. However, perhaps a more general solution would be
to use the Python locale module to format numbers according to different
locales.

I agree with you, but it seems to be hard to do.
I'm not familiar with the Python locale module and don't know how much effort
would be needed to fully internationalize the formatting.

And expose a kwarg select between the user's preferred locale,
the current U.S. English-centric defaults as they are now, or an
arbitrary locale using an ISO language code. That seems like it could
be a better long-term solution, since there are different number formats
all over, not just in Germany.

You're again right. These three possible formaters should be possible, because
the default-locale is not always the corresponding country and for scientific
publications English is needed.
My hope while sending the patch was that it could be useful to more than
German people and I didn't know about the whole problem of different
seperators.

A problem with the Python locale module might be the difference between plain
and tex-text, because for the comma as decimal seperator '{,}' is used in
tex-text and I'm not sure if the locale module take care of that.

best regards,
Matthias

···

On Wednesday 09 January 2008 18:21, Michael Droettboom wrote:

All that said, internationalization is hard -- especially for us
sheltered people in the U.S. where the defaults are most often correct.
  I may be missing an important detail here.

Cheers,
Mike

Thorsten Kranz wrote:
> Hi list, Hi Matthias,
>
> I found another way to deal with this problem. when defining the
> colorbar, one can give an additional kwarg "format", so by defining the
> kwarg "format=formatter", we solved the problem.
>
> Anyway, I think an option as Matthias implemented would be very handy
> for all those users like us here in Germany who might want to have the
> numbers formatted with commata.
>
> Greetings,
> Thorsten
>
> 2008/1/9, Matthias Michler <MatthiasMichler@…361…
> <mailto:MatthiasMichler@…361…>>:
>
> Hello list,
> Hello Thorsten,
>
> On Wednesday 09 January 2008 11:38, Thorsten Kranz wrote:
> > I have a question concerning reformatting of axis-ticks via the
> > FuncFormatter-class. In german, it's common to use a comma as
>
> separator for
>
> > decimal numbers instead of a dot. To realize it in matplotlib, I
> > do something like
> >
> > >from matplotlib.ticker import FuncFormatter
> >
> > import pylab
> > pylab.figure()
> > formatter = FuncFormatter(lambda x,pos:
> > ("%.2f"%x).replace(".",",")) ax = pylab.axes()
> > ax.xaxis.set_major_formatter(formatter)
> > ax.yaxis.set_major_formatter(formatter)
> > ax.plot(pylab.arange(0,1,0.1),pylab.arange(0,1,0.1))
> > This works fine for me,
>
> I had the same idea ;-). The problem is that you have a fixed number
> of digits
> behind the comma, which is not the desirable behaviour during zoom.
> I changed the ticker.py/ axes.py files to circumwait this
> disadvantage. I
> attached a patch showing my changes and maybe somebody can test it.
> You can activate it using:
> ax.ticklabel_format(style='comma')
> for an ScalarFormatter
>
> > but I encounter a problem when I do an
> > imshow-command with a colorbar. In the imshow-axes, it's o.k.,
>
> but for the
>
> > colorbar it doesn't really work. I do
> >
> > cb = pylab.colorbar()
> > cb.ax.yaxis.set_major_formatter(formatter)
> >
> > and, actually, all dots are replaced by com9mata, but the values
>
> are also
>
> > changed! E.g. instead of the old values (without formatter) from
>
> 0-0.54,
>
> > the
> >
> > values are increased to 0-0.95.
>
> […]
>
> > Can anyone explain why it doesn't work out as I expect it to work?
>
> I don't know were the problem comes from. I attached your example in
> a slitly
> modified version and this shows that the problem is not due to your
> special
> formatting. It occurs with matplotlib.ticker.ScalarFormatter, too.
>
> best regards,
> Matthias
>
> > Or is there a better, more standard way to substitute the dots by
>
> commata?
>
> > Thanks,
> > Thorsten
>
>
> -------------------------------------------------------------------------
> Check out the new SourceForge.net Marketplace.
> It's the best place to buy or sell services for
> just about anything Open Source.
>
> http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketpl
>ace _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> <mailto:Matplotlib-users@lists.sourceforge.net>
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
> <https://lists.sourceforge.net/lists/listinfo/matplotlib-users>
>
>
>
>
> ------------------------------------------------------------------------
>
> -------------------------------------------------------------------------
> Check out the new SourceForge.net Marketplace.
> It's the best place to buy or sell services for
> just about anything Open Source.
> http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketpl
>ace
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi Thorsten,

Hi list, Hi Matthias,

I found another way to deal with this problem. when defining the colorbar,
one can give an additional kwarg "format", so by defining the kwarg
"format=formatter", we solved the problem.

I'm happy that your problem disappeared, but I still have a question. On my
system the problem still exist, if I use "format=formatter" in colorbar and
after that use
cb.ax.yaxis.set_major_formatter(formatter)
in the example-code of my last email.
Can anybody confirm this behaviour (data range 0.0 ... 0.54 -> 0.0 ... 0.95)?
And if so: Is this the expected behaviour?

best regards,
Matthias

···

On Wednesday 09 January 2008 15:35, Thorsten Kranz wrote:

Anyway, I think an option as Matthias implemented would be very handy for
all those users like us here in Germany who might want to have the numbers
formatted with commata.

Greetings,
Thorsten

2008/1/9, Matthias Michler <MatthiasMichler@...361...>:
> Hello list,
> Hello Thorsten,
>
> On Wednesday 09 January 2008 11:38, Thorsten Kranz wrote:
> > I have a question concerning reformatting of axis-ticks via the
> > FuncFormatter-class. In german, it's common to use a comma as separator
>
> for
>
> > decimal numbers instead of a dot. To realize it in matplotlib, I do
> > something like
> >
> > >from matplotlib.ticker import FuncFormatter
> >
> > import pylab
> > pylab.figure()
> > formatter = FuncFormatter(lambda x,pos: ("%.2f"%x).replace(".",","))
> > ax = pylab.axes()
> > ax.xaxis.set_major_formatter(formatter)
> > ax.yaxis.set_major_formatter(formatter)
> > ax.plot(pylab.arange(0,1,0.1),pylab.arange(0,1,0.1))
> > This works fine for me,
>
> I had the same idea ;-). The problem is that you have a fixed number of
> digits
> behind the comma, which is not the desirable behaviour during zoom.
> I changed the ticker.py/ axes.py files to circumwait this disadvantage. I
> attached a patch showing my changes and maybe somebody can test it.
> You can activate it using:
> ax.ticklabel_format(style='comma')
> for an ScalarFormatter
>
> > but I encounter a problem when I do an
> > imshow-command with a colorbar. In the imshow-axes, it's o.k., but for
>
> the
>
> > colorbar it doesn't really work. I do
> >
> > cb = pylab.colorbar()
> > cb.ax.yaxis.set_major_formatter(formatter)
> >
> > and, actually, all dots are replaced by com9mata, but the values are
>
> also
>
> > changed! E.g. instead of the old values (without formatter) from
> > 0-0.54, the
> >
> > values are increased to 0-0.95.
>
> [...]
>
> > Can anyone explain why it doesn't work out as I expect it to work?
>
> I don't know were the problem comes from. I attached your example in a
> slitly
> modified version and this shows that the problem is not due to your
> special
> formatting. It occurs with matplotlib.ticker.ScalarFormatter, too.
>
> best regards,
> Matthias
>
> > Or is there a better, more standard way to substitute the dots by
>
> commata?
>
> > Thanks,
> > Thorsten
>
> -------------------------------------------------------------------------
> Check out the new SourceForge.net Marketplace.
> It's the best place to buy or sell services for
> just about anything Open Source.
>
> http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketpl
>ace _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi MAtthias,

yes, I can confirm this behavior. This is what I get as well. But the solution is easy: just get rid of th line

cb.ax.yaxis.set_major_formatter(formatter)

We don’t need it anymore and it messes it all up. Without it, it should work.

Greetings,
Thorsten

2008/1/11, Matthias Michler <MatthiasMichler@…361…>:

···

Hi Thorsten,

On Wednesday 09 January 2008 15:35, Thorsten Kranz wrote:

Hi list, Hi Matthias,

I found another way to deal with this problem. when defining the colorbar,
one can give an additional kwarg “format”, so by defining the kwarg

“format=formatter”, we solved the problem.

I’m happy that your problem disappeared, but I still have a question. On my
system the problem still exist, if I use “format=formatter” in colorbar and

after that use
cb.ax.yaxis.set_major_formatter(formatter)
in the example-code of my last email.
Can anybody confirm this behaviour (data range 0.0 … 0.54 -> 0.0 … 0.95)?
And if so: Is this the expected behaviour?

best regards,
Matthias

Anyway, I think an option as Matthias implemented would be very handy for
all those users like us here in Germany who might want to have the numbers
formatted with commata.

Greetings,
Thorsten

2008/1/9, Matthias Michler <MatthiasMichler@…361…>:

Hello list,
Hello Thorsten,

On Wednesday 09 January 2008 11:38, Thorsten Kranz wrote:

I have a question concerning reformatting of axis-ticks via the
FuncFormatter-class. In german, it’s common to use a comma as separator

for

decimal numbers instead of a dot. To realize it in matplotlib, I do
something like

from matplotlib.ticker
import FuncFormatter

import pylab
pylab.figure()
formatter = FuncFormatter(lambda x,pos: ("%.2f"%x).replace(".",","))

ax = pylab.axes()
ax.xaxis.set_major_formatter(formatter)
ax.yaxis.set_major_formatter(formatter)
ax.plot(pylab.arange(0,1,0.1),pylab.arange(0,1,0.1))

This works fine for me,

I had the same idea ;-). The problem is that you have a fixed number of
digits
behind the comma, which is not the desirable behaviour during zoom.

I changed the ticker.py/ axes.py files to circumwait this disadvantage. I
attached a patch showing my changes and maybe somebody can test it.
You can activate it using:
ax.ticklabel_format(style=‘comma’)
for an ScalarFormatter

but I encounter a problem when I do an
imshow-command with a colorbar. In the imshow-axes, it’s o.k., but for

the

colorbar it doesn’t really work. I do

cb = pylab.colorbar()
cb.ax.yaxis.set_major_formatter
(formatter)

and, actually, all dots are replaced by com9mata, but the values are

also

changed! E.g. instead of the old values (without formatter) from

0-0.54, the

values are increased to 0-0.95.

[…]

Can anyone explain why it doesn’t work out as I expect it to work?

I don’t know were the problem comes from. I attached your example in a
slitly
modified version and this shows that the problem is not due to your
special

formatting. It occurs with matplotlib.ticker.ScalarFormatter, too.

best regards,
Matthias

Or is there a better, more standard way to substitute the dots by

commata?

Thanks,
Thorsten


Check out the new SourceForge.net Marketplace.
It’s the best place to buy or sell services for
just about anything Open Source.


http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketpl

ace _______________________________________________
Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Check out the new SourceForge.net Marketplace.
It’s the best place to buy or sell services for
just about anything Open Source.

http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users