3 // prettyformat.py - simple Python function to format values with nice prefixes
7 // 2009 07 16: added negative number support
8 // added decimal-formatted output
10 // Copyright (c) 2009 Rolf Schmidt, Montreal
11 // rschmidt@alcor.concordia.ca
13 // This procedure is released under the GNU General Public License version 2
18 from numpy import isnan
20 def pretty_format(fValue, sUnit='', iDecimals=-1, iMultiplier=1, bLeadingSpaces=False):
26 iMultiplier=get_multiplier(fValue)
29 sUnitString = ' ' + get_prefix(iMultiplier) + sUnit
31 formatString = '% ' + repr(iLeadingSpaces + iDecimals) + '.' + repr(iDecimals) + 'f'
32 return formatString % (fValue / iMultiplier) + sUnitString
34 return str(fValue / iMultiplier) + sUnitString
37 return str(fValue / iMultiplier) + ' ' + get_prefix(fValue / iMultiplier) + sUnit
39 def get_multiplier(fValue):
40 return pow(10, get_power(fValue))
42 def get_power(fValue):
43 if fValue != 0 and not isnan(fValue):
44 #get the log10 from fValue (make sure the value is not negative)
45 dHelp = math.floor(math.log10(math.fabs(fValue)))
46 #reduce the log10 to a multiple of 3 and return it
47 return dHelp-(dHelp % 3)
51 def get_prefix(fValue):
52 #set up a dictionary to find the prefix
64 -6: lambda: u'\u00B5',
72 if fValue != 0 and not isnan(fValue):
73 #get the log10 from fValue
74 dHelp = math.floor(math.log10(math.fabs(fValue)))
77 #reduce the log10 to a multiple of 3 and create the return string
78 return prefix.get(dHelp - (dHelp % 3))()
81 dTestValue=-2.4115665714484597e-008
82 print 'Value: '+str(dTestValue)+')'
83 print 'pretty_format example (value, unit)'
84 print pretty_format(dTestValue, 'N')
85 print'-----------------------'
86 print 'pretty_format example (value, unit, decimals)'
87 print pretty_format(dTestValue, 'N', 3)
88 print'-----------------------'
89 print 'pretty_format example (value, unit, decimals, multiplier)'
90 print pretty_format(dTestValue, 'N', 5, 0.000001)
91 print'-----------------------'
92 print 'pretty_format example (value, unit, decimals, multiplier, leading spaces)'
93 print pretty_format(0.0166276297705, 'N', 3, 0.001, True)
94 print pretty_format(0.00750520813323, 'N', 3, 0.001, True)
95 print pretty_format(0.0136453282825, 'N', 3, 0.001, True)
98 #example use autoFormatValue
99 dTestValue=0.00000000567
100 print 'autoFormatValue example ('+str(dTestValue)+')'
101 print autoFormatValue(dTestValue, 'N')
105 #example use of decimalFormatValue(fValue, iDecimals, sUnit):
106 dTestValue=-2.4115665714484597e-008
108 print 'decimalFormatValue example ('+str(dTestValue)+')'
109 print decimalFormatValue(dTestValue, iDecimals, 'N')
111 #change iDecimals to see the effect
114 #example use formatValue
115 dTestValue=0.000000000567
116 print 'formatValue example ('+str(dTestValue)+')'
117 #find the (common) multiplier
118 iMultiplier=get_multiplier(dTestValue)
119 #use the multiplier and a unit to format the value
120 print formatValue(dTestValue, iMultiplier, 'N')
125 #choose any value on the axis and find the multiplier and prefix for it
126 #use those to format the rest of the scale
127 #as values can span several orders of magnitude, you have to decide what units to use
130 scaleValues=0.000000000985, 0.000000001000, 0.000000001015
131 #use this element (change to 1 or 2 to see the effect on the scale and label)
133 #get the multiplier from the value at iIndex
134 iMultiplier=get_multiplier(scaleValues[iIndex])
135 print '\nScale example'
138 for aValue in scaleValues: print decimalFormat(aValue/iMultiplier, iDecimals),
139 #print the scale label using the value at iIndex
140 print '\n'+get_prefix(scaleValues[iIndex])+'N'