|
From: Matt N. <new...@ca...> - 2004-09-08 19:10:23
|
Darren Dale wrote:
> I strongly support stripping all unnecessary zeros from the labels. It
> seems a clear choice to me, but I would be interested to know if others
> are against it.
I agree that stripping unnecessary zeros from labels is a good
idea, but I also prefer preserving significant trailing zeros, and
do not like axes reading (for example):
0.997 0.998 0.999 1 1.001 1.002
In my opinion, that '1' should be '1.000': the precision here is
significant. I also prefer more uniform series of '0.0 0.5 1.0'
to the jagged appearance of '0 0.5 1'. Maybe that's just my
interpretation of 'necessary'.
Currently, ScalarFormatter prefers '1' to '1.000'. I also see
that it uses the axis "span" to determine the formatting. I'm not
sure how easy it would be to change, but it might be better to use
the difference between axis ticks so that the formatting
guaranteed unique labels with "just enough precision".
Fortunately, matplotlib makes it easy enough to tweak the axis
formatting for those of us who obsess about such things. I've been
using FuncFormatter and the custom formatter below in a WX
plotting frame I'm working on (linear 2D plotting only so far).
I send it the list of ticks returned from the axis "major locator"
to determine the precision. The fallback is to use 0.1*span:
this is usually OK, but can be overly precise sometimes. There may
be a more efficient way of doing this, but I got a little lost in
ticker.py and axis.py....
> The regexpr was more elegant, and I don't mind spending an
> evening learning it so I can continue to work with it. On the
> other hand, string methods are more accessible. Elegant or
> accessible? What would "upper management" pick?
I think the main issue would be if using regular expressions would
be noticably faster. In this case, I'd guess not.
--Matt Newville
self.axes.xaxis.set_major_formatter(FuncFormatter(self.format_x))
self.axes.yaxis.set_major_formatter(FuncFormatter(self.format_y))
def format_x(self,x,pos):
" x-axis formatter "
xticks = self.axes.xaxis.get_major_locator()()
span = self.axes.xaxis.get_view_interval().span()
return tick_formatter(x,span,ticks=xticks,pos=pos)
def format_y(self,y,pos):
" y-axis formatter "
yticks = self.axes.yaxis.get_major_locator()()
span = self.axes.yaxis.get_view_interval().span()
return tick_formatter(y,span,ticks=yticks,pos=pos)
def tick_formatter(x, span, ticks=None,pos=0):
""" home built tick formatter to use with FuncFormatter():
x value to be formatted
span span of axis, as from viewLim.span() -- required!!!!
ticks optional list of ticks to format
"""
fmt = '%1.6e'
d = 0.1 * span
try:
d = abs(ticks[pos+1] - ticks[pos])
except:
pass
if d > 99999: fmt = '%1.6e'
elif d > 0.99: fmt = '%1.0f'
elif d > 0.099: fmt = '%1.1f'
elif d > 0.0099: fmt = '%1.2f'
elif d > 0.00099: fmt = '%1.3f'
elif d > 0.000099: fmt = '%1.4f'
elif d > 0.0000099: fmt = '%1.5f'
s = fmt % x
s.strip()
s = s.replace('+', '')
while s.find('e0')>0: s = s.replace('e0','e')
while s.find('-0')>0: s = s.replace('-0','-')
return s
|