3 """Simple Python function to format values with nice prefixes.
\r
7 from numpy import isnan
\r
10 def pretty_format(value, unit='', decimals=-1, multiplier=0, leading_spaces=False):
\r
17 leading_spaces_int = 0
\r
19 leading_spaces_int = 5
\r
20 #automatic setting of multiplier
\r
22 multiplier=get_multiplier(value)
\r
25 unit_str = ' ' + get_prefix(multiplier) + unit
\r
27 format_str = '% ' + repr(leading_spaces_int + decimals) + '.' + repr(decimals) + 'f'
\r
28 output_str = format_str % (value / multiplier) + unit_str
\r
30 output_str = str(value / multiplier) + unit_str
\r
33 output_str = str(value / multiplier) + ' ' + get_prefix(value / multiplier) + unit
\r
35 if leading_spaces_int == 0:
\r
36 output_str = output_str.lstrip()
\r
40 def get_multiplier(value):
\r
41 return pow(10, get_power(value))
\r
43 def get_power(value):
\r
44 if value != 0 and not isnan(value):
\r
45 #get the log10 from value (make sure the value is not negative)
\r
46 value_temp = math.floor(math.log10(math.fabs(value)))
\r
47 #reduce the log10 to a multiple of 3 and return it
\r
48 return value_temp - (value_temp % 3)
\r
52 def get_exponent(prefix):
\r
53 #set up a dictionary to find the exponent
\r
73 if exponent.has_key(prefix):
\r
74 return exponent[prefix]
\r
78 def get_prefix(value):
\r
79 #set up a dictionary to find the prefix
\r
91 -6: lambda: u'\u00B5',
\r
99 if value != 0 and not isnan(value):
\r
100 #get the log10 from value
\r
101 value_temp = math.floor(math.log10(math.fabs(value)))
\r
104 #reduce the log10 to a multiple of 3 and create the return string
\r
105 return prefix.get(value_temp - (value_temp % 3))()
\r
108 test_value=-2.4115665714484597e-008
\r
109 print 'Value: '+str(test_value)+')'
\r
110 print 'pretty_format example (value, unit)'
\r
111 print pretty_format(test_value, 'N')
\r
112 print'-----------------------'
\r
113 print 'pretty_format example (value, unit, decimals)'
\r
114 print pretty_format(test_value, 'N', 3)
\r
115 print'-----------------------'
\r
116 print 'pretty_format example (value, unit, decimals, multiplier)'
\r
117 print pretty_format(test_value, 'N', 5, 0.000001)
\r
118 print'-----------------------'
\r
119 print 'pretty_format example (value, unit, decimals, multiplier, leading spaces)'
\r
120 print pretty_format(0.0166276297705, 'N', 3, 0.001, True)
\r
121 print pretty_format(0.00750520813323, 'N', 3, 0.001, True)
\r
122 print pretty_format(0.0136453282825, 'N', 3, 0.001, True)
\r
125 #to output a scale:
\r
126 #choose any value on the axis and find the multiplier and prefix for it
\r
127 #use those to format the rest of the scale
\r
128 #as values can span several orders of magnitude, you have to decide what units to use
\r
131 scale_values=0.000000000985, 0.000000001000, 0.000000001015
\r
132 #use this element (change to 1 or 2 to see the effect on the scale and label)
\r
134 #get the multiplier from the value at index
\r
135 multiplier=get_multiplier(scale_values[index])
\r
136 print '\nScale example'
\r
139 for aValue in scale_values: print decimalFormat(aValue/multiplier, decimals),
\r
140 #print the scale label using the value at index
\r
141 print '\n'+get_prefix(scale_values[index])+'N'
\r