Pylab breaks locales with GTK

Hi all,
Trying to write to text files some plotted datas, we have a strange
behavour on masked arrays after importing pylab, with the dot decimal
separator replaced by a comma (but not all) :

···

##############################
Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

import numpy as np
a=np.ma.array([1., 1.01, 2.11, 3.61])
for i in a:str(i)

...
'1.0'
'1.01'
'2.11'
'3.61'

import matplotlib
for i in a:str(i)

...
'1.0'
'1.01'
'2.11'
'3.61'

import pylab
for i in a:str(i)

...
'1.0'
'1,01'
'2,11'
'3,61'

np.__version__, matplotlib.__version__

('1.2.1', '0.98.3')
#############################

Any help welcomes!

--
Lionel Roubeyrie
chargé d'études
LIMAIR - La Surveillance de l'Air en Limousin
http://www.limair.asso.fr

Lionel Roubeyrie wrote:

Hi all,
Trying to write to text files some plotted datas, we have a strange
behavour on masked arrays after importing pylab, with the dot decimal
separator replaced by a comma (but not all) :
##############################
Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

This has been reported before, and IIRC, it's a problem with PyGTK.

Ryan

···

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

Hi Ryan,

Ryan May wrote:

Lionel Roubeyrie wrote:
  

Hi all,
Trying to write to text files some plotted datas, we have a strange
behavour on masked arrays after importing pylab, with the dot decimal
separator replaced by a comma (but not all) :
##############################
Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
    
This has been reported before, and IIRC, it's a problem with PyGTK.
  
The fact that pygtk changes the local at import sounds buggy indeed, but
numpy should not be dependent on the locale anyway, so it also shows a
numpy bug I think:

http://scipy.org/scipy/numpy/ticket/884

Pauli and other have been working on those locale-related bugs in numpy,
but they are a bit fastidious to fix - hopefully, they will make their
way into numpy 1.3

David

This seems to be a bug (or at least inconsistent behavior) in numpy when the locale is set (which happens when gtk is imported -- replace 'import pylab' with 'import gtk' and you'll see the same behavior).

We actually use a workaround for this in other parts of matplotlib, which is:

  for i in a: str(float(i))

By forcing the conversion to a Python float first, you can get around the localized number output.

I'll follow up with this on the numpy bug tracker or mailing list, since this has been a long-standing niggle of mine as well.

Cheers,
Mike

Lionel Roubeyrie wrote:

···

Hi all,
Trying to write to text files some plotted datas, we have a strange
behavour on masked arrays after importing pylab, with the dot decimal
separator replaced by a comma (but not all) : ##############################
Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49) [GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
  

import numpy as np
a=np.ma.array([1., 1.01, 2.11, 3.61])
for i in a:str(i)
        

... '1.0'
'1.01'
'2.11'
'3.61'
  

import matplotlib
for i in a:str(i)
        

... '1.0'
'1.01'
'2.11'
'3.61'
  

import pylab
for i in a:str(i)
        

... '1.0'
'1,01'
'2,11'
'3,61'
  

np.__version__, matplotlib.__version__
        

('1.2.1', '0.98.3')
#############################

Any help welcomes!

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

It's a bug with PyGTK in that merely importing it sets the locale.

But more seriously, it's also a bug in Numpy, in that its string formatting is dependent on locale (unlike standard floats in Python).

See this Numpy bug:

http://projects.scipy.org/scipy/numpy/ticket/902

···

*
*and this mailing list thread:

http://www.mail-archive.com/numpy-discussion@...177.../msg14563.html

Cheers,
Mike

Ryan May wrote:

Lionel Roubeyrie wrote:
  

Hi all,
Trying to write to text files some plotted datas, we have a strange
behavour on masked arrays after importing pylab, with the dot decimal
separator replaced by a comma (but not all) : ##############################
Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49) [GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
    
This has been reported before, and IIRC, it's a problem with PyGTK.

Ryan

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

Thanks for your responses,
Looking comments in the tickets, putting
import locale
locale.setlocale(locale.LC_NUMERIC, 'C')
after the pylab import resolves the problem (but not the bug...).

Have a happy new year

···

Le lundi 05 janvier 2009 à 15:37 +0100, Lionel Roubeyrie a écrit :

Hi all,
Trying to write to text files some plotted datas, we have a strange
behavour on masked arrays after importing pylab, with the dot decimal
separator replaced by a comma (but not all) :
##############################
Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a=np.ma.array([1., 1.01, 2.11, 3.61])
>>> for i in a:str(i)
...
'1.0'
'1.01'
'2.11'
'3.61'
>>> import matplotlib
>>> for i in a:str(i)
...
'1.0'
'1.01'
'2.11'
'3.61'
>>> import pylab
>>> for i in a:str(i)
...
'1.0'
'1,01'
'2,11'
'3,61'
>>> np.__version__, matplotlib.__version__
('1.2.1', '0.98.3')
#############################

Any help welcomes!

--
Lionel Roubeyrie
chargé d'études
LIMAIR - La Surveillance de l'Air en Limousin
http://www.limair.asso.fr