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
Index: ticker.py
===================================================================
--- ticker.py (revision 4805)
+++ ticker.py (working copy)
@@ -272,13 +272,15 @@
such that the tick labels are meaningful. Scientific notation is used for
data < 1e-3 or data >= 1e4.
"""
- def __init__(self, useOffset=True, useMathText=False):
+ def __init__(self, useOffset=True, useMathText=False, useComma=False):
# 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
+ # useComma leads to comma (',') as decimal separator
self._useOffset = useOffset
self._usetex = rcParams['text.usetex']
self._useMathText = useMathText
+ self._useComma = useComma
self.offset = 0
self.orderOfMagnitude = 0
self.format = ''
@@ -327,11 +329,17 @@
if self.offset:
offsetStr = self.format_data(self.offset)
if self.offset > 0: offsetStr = '+' + offsetStr
+ if self._useComma:
+ if self._usetex:
+ offsetStr = offsetStr.replace('.','{,}')
+ else:
+ offsetStr = offsetStr.replace('.',',')
if self.orderOfMagnitude:
if self._usetex or self._useMathText:
sciNotStr = r'{\times}'+self.format_data(10**self.orderOfMagnitude)
else:
sciNotStr = u'\xd7'+'1e%d'% self.orderOfMagnitude
+
if self._useMathText:
return ''.join(('$\mathdefault{',sciNotStr,offsetStr,'}$'))
elif self._usetex:
@@ -406,7 +414,13 @@
def pprint_val(self, x):
xp = (x-self.offset)/10**self.orderOfMagnitude
if npy.absolute(xp) < 1e-8: xp = 0
- return self.format % xp
+ if self._useComma:
+ if self._usetex:
+ return (self.format % xp).replace('.','{,}')
+ else:
+ return (self.format % xp).replace('.',',')
+ else:
+ return self.format % xp
def _formatSciNotation(self, s):
# transform 1e+004 into 1e4, for example
Index: axes.py
===================================================================
--- axes.py (revision 4805)
+++ axes.py (working copy)
@@ -1438,8 +1438,9 @@
used by default for linear axes.
kwargs:
- style = 'sci' (or 'scientific') or 'plain';
- plain turns off scientific notation
+ style = 'sci' (or 'scientific') or 'plain' or 'comma';
+ plain/comma turns off scientific notation
+ comma enables decimal seperator ','
axis = 'x', 'y', or 'both'
Only the major ticks are affected.
@@ -1452,15 +1453,13 @@
"""
style = kwargs.pop('style', '').lower()
axis = kwargs.pop('axis', 'both').lower()
+ cb = False
if style[:3] == 'sci':
sb = True
elif style in ['plain', 'comma']:
sb = False
- if style == 'plain':
- cb = False
- else:
+ if style == 'comma':
cb = True
- raise NotImplementedError, "comma style remains to be added"
elif style == '':
sb = None
else:
@@ -1468,8 +1467,10 @@
if sb is not None:
if axis == 'both' or axis == 'x':
self.xaxis.major.formatter.set_scientific(sb)
+ self.xaxis.major.formatter._useComma = cb
if axis == 'both' or axis == 'y':
self.yaxis.major.formatter.set_scientific(sb)
+ self.yaxis.major.formatter._useComma = cb
def set_axis_off(self):
"""
example_of_thorsten_kranz.py
Description: application/python
------------------------------------------------------------------------- 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 [email protected] https://lists.sourceforge.net/lists/listinfo/matplotlib-users
