From: Darren Dale <dd55@co...>  20050228 01:40:50

oops, I just noticed a bug, the first script I posted wont run. This updated script worked for me with a fresh 0.72.1 installation. Sorry about the error. Darren from matplotlib import * rc('font',size='smaller') rc('tick',labelsize='smaller') from matplotlib.ticker import ScalarFormatter, LinearLocator import math from matplotlib.numerix import absolute, average from pylab import * class ScalarFormatterScientific(ScalarFormatter): """ Tick location is a plain old number. If useOffset==True and the data range <1e4* the data average, then an offset will be determined such that the tick labels are meaningful. Scientific notation is used for data < 1e4 or data >= 1e4. Scientific notation is presented once for each axis, in the last ticklabel. """ def __init__(self, useOffset=True): """ useOffset allows plotting small data ranges with large offsets: for example: [1+1e9,1+2e9,1+3e9] """ self._useOffset = useOffset self.offset = 0 self.orderOfMagnitude = 0 self.format = None def set_locs(self, locs): self.locs = locs self._set_offset() self._set_orderOfMagnitude() self._set_format() def _set_offset(self): # offset of 20,001 is 20,000, for example if self._useOffset: ave_loc = average(self.locs) std_loc = std(self.locs) if ave_loc: # dont want to take log10(0) ave_oom = math.floor(math.log10(absolute(ave_loc))) if std_loc/math.fabs(ave_loc) < 1e4: # four sigfigs # add 1e15 because of floating point precision, fixes conversion self.offset = int(ave_loc/10**ave_oom+1e15)*10**ave_oom else: self.offset = 0 def _set_orderOfMagnitude(self): # if scientific notation is to be used, find the appropriate exponent # if using an offset, find the OOM after applying the offset locs = array(self.locs)self.offset ave_loc_abs = average(absolute(locs)) oom = math.floor(math.log10(ave_loc_abs)) # need to specialcase for range of 01e5 if oom <= 0 and std(locs) < 1e4:#10**(2*oom): self.orderOfMagnitude = oom elif oom <=0 and oom >= 5: pass elif math.fabs(oom) >= 4: self.orderOfMagnitude = oom def _set_format(self): # set the format string to format all the ticklabels locs = (array(self.locs,'d')self.offset) / \ 10**self.orderOfMagnitude+1e15 sigfigs = [len(str('%1.4f'% loc).split('.')[1].rstrip('0')) \ for loc in locs] sigfigs.sort() self.format = '%1.' + str(sigfigs[1]) + 'f' def pprint_val(self, x, d): # d is no longer necessary, x is the tick location. xp = (xself.offset)/10**self.orderOfMagnitude if x==self.locs[1] and (self.orderOfMagnitude or self.offset): offsetStr = '' sciNotStr = '' xp = self.format % xp if self.offset: p = '%1.e+'% self.offset offsetStr = self._formatSciNotation(p) if self.orderOfMagnitude: p = 'x%1.e'% 10**self.orderOfMagnitude sciNotStr = self._formatSciNotation(p) return ''.join((offsetStr,xp,sciNotStr[2:])) elif xp==0: return '%d'% xp else: return self.format % xp def _formatSciNotation(self,s): # transform 1e+004 into 1e4, for example tup = s.split('e') mantissa = tup[0] sign = tup[1][0].replace('+', '') exponent = tup[1][1:].lstrip('0') return '%se%s%s' %(mantissa, sign, exponent) figure(1,figsize=(6,6)) ax1 = axes([.2,.74,.75,.2]) ax1.plot(arange(11)*5e2) ax1.yaxis.set_major_formatter(ScalarFormatterScientific()) ax1.xaxis.set_visible(False) ax1.set_title('BIG NUMBERS',fontsize=14) ax2 = axes([.2,.51,.75,.2]) ax2.plot(arange(11)*1e4) ax2.yaxis.set_major_formatter(ScalarFormatterScientific()) ax2.text(1,6e4,'y=1e4*x') ax2.xaxis.set_visible(False) ax3 = axes([.2,.28,.75,.2]) ax3.plot(arange(11)*1e4+1e10) ax3.yaxis.set_major_formatter(ScalarFormatterScientific()) ax3.text(1,6e4+1e10,'y=1e4*x+1e10') ax3.xaxis.set_visible(False) ax4 = axes([.2,.05,.75,.2]) ax4.plot(arange(11)*1e4+1e10) ax4.yaxis.set_major_formatter(ScalarFormatterScientific(useOffset=False)) ax4.text(1,1e10+6e4,'y=1e4*x+1e10, no offset') figure(2,figsize=(6,6)) ax1 = axes([.225,.74,.75,.2]) ax1.plot(arange(11)*1e4) ax1.yaxis.set_major_formatter(ScalarFormatterScientific()) ax1.xaxis.set_visible(False) ax1.set_title('small numbers',fontsize=8) ax2 = axes([.225,.51,.75,.2]) ax2.plot(arange(11)*1e5) ax2.yaxis.set_major_formatter(ScalarFormatterScientific()) ax2.text(1,6e5,'y=1e5*x') ax2.xaxis.set_visible(False) ax3 = axes([.225,.28,.75,.2]) ax3.plot(arange(11)*1e10+1e5) ax3.yaxis.set_major_formatter(ScalarFormatterScientific()) ax3.text(1,1e5+6e10,'y=1e10*x+1e5') ax3.xaxis.set_visible(False) ax4 = axes([.225,.05,.75,.2]) ax4.plot(arange(11)*1e10+1e5) ax4.yaxis.set_major_formatter(ScalarFormatterScientific(useOffset=False)) ax4.text(1,1e5+6e10,'y=1e10*x+1e5, no offset') show() 