Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6594083

Browse files
committed
Merge branch 'locale_formatting' of https://github.com/mdboom/matplotlib into mdboom-locale_formatting
2 parents 33ee6ee + ab8283f commit 6594083

7 files changed

Lines changed: 65 additions & 8 deletions

File tree

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
2011-08-18 Change api of Axes.get_tightbbox and add an optional
22
keyword parameter *call_axes_locator*. - JJL
33

4+
2011-07-29 A new rcParam "axes.formatter.use_locale" was added, that,
5+
when True, will use the current locale to format tick
6+
labels. This means that, for example, in the fr_FR locale,
7+
',' will be used as a decimal separator. - MGD
8+
49
2011-07-15 The set of markers available in the plot() and scatter()
510
commands has been unified. In general, this gives more
611
options to both than were previously available, however,
@@ -11,6 +16,7 @@
1116
diamond". "D" can be used for a "diamond".
1217

1318
-MGD
19+
1420
2011-07-13 Fix numerical problems in symlog scale, particularly when
1521
linthresh <= 1.0. Symlog plots may look different if one
1622
was depending on the old broken behavior - MGD

lib/matplotlib/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,9 @@ def rc_params(fail_on_error=False):
786786
rcParams['ps.usedistiller'] = checkdep_ps_distiller(rcParams['ps.usedistiller'])
787787
rcParams['text.usetex'] = checkdep_usetex(rcParams['text.usetex'])
788788

789-
789+
if rcParams['axes.formatter.use_locale']:
790+
import locale
791+
locale.setlocale(locale.LC_ALL, '')
790792

791793
def rc(group, **kwargs):
792794
"""

lib/matplotlib/axes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,6 +2108,13 @@ def ticklabel_format(self, **kwargs):
21082108
numeric offset is specified, it will be
21092109
used.
21102110
*axis* [ 'x' | 'y' | 'both' ]
2111+
*useLocale* If True, format the number according to
2112+
the current locale. The affects things
2113+
such as the character used for the
2114+
decimal separator. If False, use
2115+
C-style (English) formatting. The
2116+
default setting is controlled by the
2117+
axes.formatter.use_locale rcparam.
21112118
============ =========================================
21122119
21132120
Only the major ticks are affected.
@@ -2120,6 +2127,7 @@ def ticklabel_format(self, **kwargs):
21202127
style = kwargs.pop('style', '').lower()
21212128
scilimits = kwargs.pop('scilimits', None)
21222129
useOffset = kwargs.pop('useOffset', None)
2130+
use_locale = kwargs.pop('useLocale', None)
21232131
axis = kwargs.pop('axis', 'both').lower()
21242132
if scilimits is not None:
21252133
try:
@@ -2156,6 +2164,11 @@ def ticklabel_format(self, **kwargs):
21562164
self.xaxis.major.formatter.set_useOffset(useOffset)
21572165
if axis == 'both' or axis == 'y':
21582166
self.yaxis.major.formatter.set_useOffset(useOffset)
2167+
if useLocale is not None:
2168+
if axis == 'both' or axis == 'x':
2169+
self.xaxis.major.formatter.set_useLocale(useLocale)
2170+
if axis == 'both' or axis == 'y':
2171+
self.yaxis.major.formatter.set_useLocale(useLocale)
21592172
except AttributeError:
21602173
raise AttributeError(
21612174
"This method only works with the ScalarFormatter.")

lib/matplotlib/cbook.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
# side effects. Passing False eliminates those side effects.
3030

3131
try:
32-
preferredencoding = locale.getpreferredencoding(False).strip()
32+
preferredencoding = locale.getpreferredencoding(
33+
matplotlib.rcParams['axes.formatter.use_locale']).strip()
3334
if not preferredencoding:
3435
preferredencoding = None
3536
except (ValueError, ImportError, AttributeError):

lib/matplotlib/rcsetup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ def __call__(self, s):
449449
# use scientific notation if log10
450450
# of the axis range is smaller than the
451451
# first or larger than the second
452+
'axes.formatter.use_locale' : [False, validate_bool], # Use the current locale to format ticks
452453
'axes.unicode_minus' : [True, validate_bool],
453454
'axes.color_cycle' : [['b','g','r','c','m','y','k'],
454455
validate_colorlist], # cycle of plot

lib/matplotlib/ticker.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126

127127
from __future__ import division
128128
import decimal
129+
import locale
129130
import math
130131
import numpy as np
131132
from 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':

matplotlibrc.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ backend : %(backend)s
212212
#axes.formatter.limits : -7, 7 # use scientific notation if log10
213213
# of the axis range is smaller than the
214214
# first or larger than the second
215+
#axes.formatter.use_locale : False # When True, format tick labels according to the user's locale.
216+
# For example, use ',' as a decimal separator in the fr_FR locale.
215217
#axes.unicode_minus : True # use unicode for the minus symbol
216218
# rather than hypen. See http://en.wikipedia.org/wiki/Plus_sign#Plus_sign
217219
#axes.color_cycle : b, g, r, c, m, y, k # color cycle for plot lines

0 commit comments

Comments
 (0)