126126
127127from __future__ import division
128128import decimal
129+ import locale
129130import math
130131import numpy as np
131132from matplotlib import rcParams
@@ -330,7 +331,7 @@ class ScalarFormatter(Formatter):
330331
331332 """
332333
333- def __init__ (self , useOffset = True , useMathText = False ):
334+ def __init__ (self , useOffset = True , useMathText = False , useLocale = None ):
334335 # useOffset allows plotting small data ranges with large offsets:
335336 # for example: [1+1e-9,1+2e-9,1+3e-9]
336337 # useMathText will render the offset and scientific notation in mathtext
@@ -341,6 +342,10 @@ def __init__(self, useOffset=True, useMathText=False):
341342 self .format = ''
342343 self ._scientific = True
343344 self ._powerlimits = rcParams ['axes.formatter.limits' ]
345+ if useLocale is None :
346+ self ._useLocale = rcParams ['axes.formatter.use_locale' ]
347+ else :
348+ self ._useLocale = useLocale
344349
345350 def get_useOffset (self ):
346351 return self ._useOffset
@@ -355,6 +360,17 @@ def set_useOffset(self, val):
355360
356361 useOffset = property (fget = get_useOffset , fset = set_useOffset )
357362
363+ def get_useLocale (self ):
364+ return self ._useLocale
365+
366+ def set_useLocale (self , val ):
367+ if val is None :
368+ self ._useLocale = rcParams ['axes.formatter.use_locale' ]
369+ else :
370+ self ._useLocale = val
371+
372+ useLocale = property (fget = get_useLocale , fset = set_useLocale )
373+
358374 def fix_minus (self , s ):
359375 'use a unicode minus rather than hyphen'
360376 if rcParams ['text.usetex' ] or not rcParams ['axes.unicode_minus' ]: return s
@@ -388,11 +404,18 @@ def set_powerlimits(self, lims):
388404
389405 def format_data_short (self ,value ):
390406 'return a short formatted string representation of a number'
391- return '%-12g' % value
407+ if self ._useLocale :
408+ return locale .format_string ('%-12g' , (value ,))
409+ else :
410+ return '%-12g' % value
392411
393412 def format_data (self ,value ):
394413 'return a formatted string representation of a number'
395- s = self ._formatSciNotation ('%1.10e' % value )
414+ if self ._useLocale :
415+ s = locale .format_string ('%1.10e' , (value ,))
416+ else :
417+ s = '%1.10e' % value
418+ s = self ._formatSciNotation (s )
396419 return self .fix_minus (s )
397420
398421
@@ -491,14 +514,23 @@ def _set_format(self):
491514 def pprint_val (self , x ):
492515 xp = (x - self .offset )/ 10 ** self .orderOfMagnitude
493516 if np .absolute (xp ) < 1e-8 : xp = 0
494- return self .format % xp
517+ if self ._useLocale :
518+ return locale .format_string (self .format , (xp ,))
519+ else :
520+ return self .format % xp
495521
496522 def _formatSciNotation (self , s ):
497523 # transform 1e+004 into 1e4, for example
524+ if self ._useLocale :
525+ decimal_point = locale .localeconv ()['decimal_point' ]
526+ positive = locale .localeconv ()['positive_sign' ]
527+ else :
528+ decimal_point = '.'
529+ positive_sign = '+'
498530 tup = s .split ('e' )
499531 try :
500- significand = tup [0 ].rstrip ('0' ).rstrip ('.' )
501- sign = tup [1 ][0 ].replace ('+' , '' )
532+ significand = tup [0 ].rstrip ('0' ).rstrip (decimal_point )
533+ sign = tup [1 ][0 ].replace (positive_sign , '' )
502534 exponent = tup [1 ][1 :].lstrip ('0' )
503535 if self ._useMathText or self ._usetex :
504536 if significand == '1' :
0 commit comments