Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2011-07-29 A new rcParam "axes.formatter.use_locale" was added, that,
when True, will use the current locale to formatter tick
labels. This means that, for example, in the fr_FR locale,
',' will be used as a decimal separator. - MGD

2011-07-15 The set of markers available in the plot() and scatter()
commands has been unified. In general, this gives more
options to both than were previously available, however,
Expand Down
4 changes: 3 additions & 1 deletion lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,9 @@ def rc_params(fail_on_error=False):
rcParams['ps.usedistiller'] = checkdep_ps_distiller(rcParams['ps.usedistiller'])
rcParams['text.usetex'] = checkdep_usetex(rcParams['text.usetex'])


if rcParams['axes.formatter.use_locale']:
import locale
locale.setlocale(locale.LC_ALL, '')

def rc(group, **kwargs):
"""
Expand Down
13 changes: 13 additions & 0 deletions lib/matplotlib/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2108,6 +2108,13 @@ def ticklabel_format(self, **kwargs):
numeric offset is specified, it will be
used.
*axis* [ 'x' | 'y' | 'both' ]
*useLocale* If True, format the number according to
the current locale. The affects things
such as the character used for the
decimal separator. If False, use
C-style (English) formatting. The
default setting is controlled by the
axes.formatter.use_locale rcparam.
============ =========================================

Only the major ticks are affected.
Expand All @@ -2120,6 +2127,7 @@ def ticklabel_format(self, **kwargs):
style = kwargs.pop('style', '').lower()
scilimits = kwargs.pop('scilimits', None)
useOffset = kwargs.pop('useOffset', None)
use_locale = kwargs.pop('useLocale', None)
axis = kwargs.pop('axis', 'both').lower()
if scilimits is not None:
try:
Expand Down Expand Up @@ -2156,6 +2164,11 @@ def ticklabel_format(self, **kwargs):
self.xaxis.major.formatter.set_useOffset(useOffset)
if axis == 'both' or axis == 'y':
self.yaxis.major.formatter.set_useOffset(useOffset)
if useLocale is not None:
if axis == 'both' or axis == 'x':
self.xaxis.major.formatter.set_useLocale(useLocale)
if axis == 'both' or axis == 'y':
self.yaxis.major.formatter.set_useLocale(useLocale)
except AttributeError:
raise AttributeError(
"This method only works with the ScalarFormatter.")
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
# side effects. Passing False eliminates those side effects.

try:
preferredencoding = locale.getpreferredencoding(False).strip()
preferredencoding = locale.getpreferredencoding(
matplotlib.rcParams['axes.formatter.use_locale']).strip()
if not preferredencoding:
preferredencoding = None
except (ValueError, ImportError, AttributeError):
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ def __call__(self, s):
# use scientific notation if log10
# of the axis range is smaller than the
# first or larger than the second
'axes.formatter.use_locale' : [False, validate_bool], # Use the current locale to format ticks
'axes.unicode_minus' : [True, validate_bool],
'axes.color_cycle' : [['b','g','r','c','m','y','k'],
validate_colorlist], # cycle of plot
Expand Down
44 changes: 38 additions & 6 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@

from __future__ import division
import decimal
import locale
import math
import numpy as np
from matplotlib import rcParams
Expand Down Expand Up @@ -330,7 +331,7 @@ class ScalarFormatter(Formatter):

"""

def __init__(self, useOffset=True, useMathText=False):
def __init__(self, useOffset=True, useMathText=False, useLocale=None):
# useOffset allows plotting small data ranges with large offsets:
# for example: [1+1e-9,1+2e-9,1+3e-9]
# useMathText will render the offset and scientific notation in mathtext
Expand All @@ -341,6 +342,10 @@ def __init__(self, useOffset=True, useMathText=False):
self.format = ''
self._scientific = True
self._powerlimits = rcParams['axes.formatter.limits']
if useLocale is None:
self._useLocale = rcParams['axes.formatter.use_locale']
else:
self._useLocale = useLocale

def get_useOffset(self):
return self._useOffset
Expand All @@ -355,6 +360,17 @@ def set_useOffset(self, val):

useOffset = property(fget=get_useOffset, fset=set_useOffset)

def get_useLocale(self):
return self._useLocale

def set_useLocale(self, val):
if val is None:
self._useLocale = rcParams['axes.formatter.use_locale']
else:
self._useLocale = val

useLocale = property(fget=get_useLocale, fset=set_useLocale)

def fix_minus(self, s):
'use a unicode minus rather than hyphen'
if rcParams['text.usetex'] or not rcParams['axes.unicode_minus']: return s
Expand Down Expand Up @@ -388,11 +404,18 @@ def set_powerlimits(self, lims):

def format_data_short(self,value):
'return a short formatted string representation of a number'
return '%-12g'%value
if self._useLocale:
return locale.format_string('%-12g', (value,))
else:
return '%-12g'%value

def format_data(self,value):
'return a formatted string representation of a number'
s = self._formatSciNotation('%1.10e'% value)
if self._useLocale:
s = locale.format_string('%1.10e', (value,))
else:
s = '%1.10e' % value
s = self._formatSciNotation(s)
return self.fix_minus(s)


Expand Down Expand Up @@ -491,14 +514,23 @@ def _set_format(self):
def pprint_val(self, x):
xp = (x-self.offset)/10**self.orderOfMagnitude
if np.absolute(xp) < 1e-8: xp = 0
return self.format % xp
if self._useLocale:
return locale.format_string(self.format, (xp,))
else:
return self.format % xp

def _formatSciNotation(self, s):
# transform 1e+004 into 1e4, for example
if self._useLocale:
decimal_point = locale.localeconv()['decimal_point']
positive = locale.localeconv()['positive_sign']
else:
decimal_point = '.'
positive_sign = '+'
tup = s.split('e')
try:
significand = tup[0].rstrip('0').rstrip('.')
sign = tup[1][0].replace('+', '')
significand = tup[0].rstrip('0').rstrip(decimal_point)
sign = tup[1][0].replace(positive_sign, '')
exponent = tup[1][1:].lstrip('0')
if self._useMathText or self._usetex:
if significand == '1':
Expand Down
2 changes: 2 additions & 0 deletions matplotlibrc.template
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ backend : %(backend)s
#axes.formatter.limits : -7, 7 # use scientific notation if log10
# of the axis range is smaller than the
# first or larger than the second
#axes.formatter.use_locale : False # When True, format tick labels according to the user's locale.
# For example, use ',' as a decimal separator in the fr_FR locale.
#axes.unicode_minus : True # use unicode for the minus symbol
# rather than hypen. See http://en.wikipedia.org/wiki/Plus_sign#Plus_sign
#axes.color_cycle : b, g, r, c, m, y, k # color cycle for plot lines
Expand Down