From: John H. <jdh...@ac...> - 2004-02-04 03:12:58
|
>>>>> "matthew" == matthew arnison <ma...@ca...> writes: matthew> Hi again, I'm having more trouble with matplotlib ticks matthew> today. I wrote a little demo script that illustrates some matthew> of the problems: Hi Matthew, Thanks for sending me these example scripts - it really helps to have complete examples when working on these problems. I've made a few changes to the tickval formatter. The relevant function is matplotlib.axis.format_tickval and is pretty simple conceptually. Try replacing the default format_tickval with this one. The d variable gives the max-min distance of the view limits. I use different format strings depending on the size of the distance. def format_tickval(self, x): 'Format the number x as a string' d = self.viewlim.interval() if self._scale == 'log': # only label the decades fx = self.transData.func(x) isdecade = abs(fx-int(fx))<1e-10 if self._ticklabelStrings is None and not isdecade: return '' #if the number is not too big and it's an int, format it as an #int if abs(x)<1e4 and x==int(x): return '%d' % x # if the value is just a fraction off an int, use the int if abs(x-int(x))<0.0001*d: return '%d' % int(x) # use exponential formatting for really big or small numbers, # else use float if d < 1e-2 : fmt = '%1.2e' elif d < 1e-1 : fmt = '%1.2f' elif d > 1e5 : fmt = '%1.3e' elif d > 10 : fmt = '%1.1f' elif d > 1 : fmt = '%1.2f' else : fmt = '%1.3f' s = fmt % x #print d, fmt, x, s # strip trailing zeros, remove '+', and handle exponential formatting m = self._zerorgx.match(s) if m: s = m.group(1) if m.group(2) is not None: s += m.group(2) s = s.replace('+', '') return s And then feed it some more of your sadistic examples <wink>. If you don't like what you see, try tweaking the formats and the distance values until you get sensible results. Or feel free to provide more comments and send more examples. JDH |