SourceForge logo
SourceForge logo
Menu

matplotlib-devel

From: Jamil K. <jam...@ca...> - 2005年07月27日 04:03:16
Hello,
A few posts back, I included source code to some gauges I had whipped =
together. After some constructive advice from John Hunter (Thanks!), =
I've had time to polish them a bit and include the logarithmic ones as =
promised.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D
#!/usr/bin/env python
"""
The Log_Meter widget draws a logarithmic meter, either horizontally or =
vertically. You supply the direction, limits,
shaded regions, names and the current value, and invoke it like this:
 from pylab import figure, show
 =20
 raw_value =3D -4.0
 raw_limits =3D [-10.0,10.0,5,1]
 raw_zones =3D [[-10.0,0.0,'r'],[0.0,5.0,'y'],[5.0,10.0,'g']]
 attribute_name =3D "Rx MOS (24h)"
 =20
 s_length =3D 0.3
 p_length =3D 2.0
 fig_height =3D s_length + 1.0
 fig_width =3D p_length + 0.4
 fig =3D figure( figsize=3D(fig_width, fig_height) )
 =20
 rect =3D [(0.2/fig_width), (0.5/fig_height),
 (p_length/fig_width), (s_length/fig_height)]
 =20
 meter =3D Log_Meter(fig, rect,
 xlim=3D( -0.1, p_length+0.1 ),
 ylim=3D( -0.4, s_length+0.1 ),
 xticks=3D[],
 yticks=3D[],
 )
 meter.set_axis_off()
 fig.add_axes(meter)
 show()
"""
from __future__ import division
from matplotlib.figure import Figure
from matplotlib.axes import Axes
import math
import types
from math import pi
class Meter(Axes):
 def __init__(self, vertical, raw_value, raw_limits, raw_zones, =
attribute_name, field_names, file_name, resolution, p_length, s_length, =
*args, **kwargs):
 Axes.__init__(self, *args, **kwargs)
 #Perform Checking
 if( raw_limits[0] =3D=3D raw_limits[1] ):
 raise ValueError('identical_limits_exception: %s' % =
raw_limits)
 if( raw_limits[1] > raw_limits[0] ):
 self.graph_positive =3D True
 else: #Swap the limits around
 self.graph_positive =3D False
 raw_limits[0], raw_limits[1] =3D raw_limits[1] =3D =
raw_limits[0]
 =20
 if not( math.log10(raw_limits[0]) % 1.0 =3D=3D 0 and =
math.log10(raw_limits[1]) % 1.0 =3D=3D 0 ):
 raise ValueError('bad_limits_exception:%s' % raw_limits)
 for zone in raw_zones:
 if( zone[0] > zone[1] ):	#Swap the zones so zone[1] > =
zone[0]
 zone[0], zone[1] =3D zone[1] =3D zone[0]
 if( zone[1] < raw_limits[0] or zone[0] > raw_limits[1] =
):
 raise ValueError('bad_zone_exception'%zone)
 if( zone[0] < raw_limits[0] ):
 zone[0] =3D raw_limits[0]
 if( zone[1] > raw_limits[1] ):
 zone[1] =3D raw_limits[1]
 =20
 =20
 =20
 #Stuff all of the variables into self.
 self.vertical =3D vertical
 self.raw_value =3D raw_value
 self.raw_limits =3D raw_limits
 self.raw_zones =3D raw_zones
 self.attribute_name =3D attribute_name
 self.field_names =3D field_names
 self.file_name =3D file_name
 self.resolution =3D resolution
 self.p_length =3D p_length
 self.s_length =3D s_length
 =20
 #Draw the meter
 self.graph_center =3D =
((math.log10(self.raw_limits[0])+math.log10(self.raw_limits[1]))/2)
 for zone in raw_zones:
 self.draw_bar( zone, False)
 self.draw_bar( None, True)
 self.draw_ticks()
 self.draw_needle()
 if( vertical ):
 self.text( self.s_length/2, =
math.log10(self.raw_limits[0])-0.2, self.attribute_name, size=3D12, =
va=3D'top', ha=3D'center')
 else:
 self.text( self.graph_center, self.s_length+0.25, =
self.attribute_name, size=3D12, va=3D'bottom', ha=3D'center')
 =20
 def draw_bar( self, zone, border ):
 if( border ):
 start =3D self.raw_limits[0]
 end =3D self.raw_limits[1]
 else:
 start =3D zone[0]
 end =3D zone[1]
 colour =3D zone[2]
 =20
 if( self.graph_positive ):
 start =3D math.log10(start)
 end =3D math.log10(end)
 else:
 start =3D -math.log10(start)
 end =3D -math.log10(end)
 =20
 s_vect =3D [ 0.0, 0.0, self.s_length, self.s_length ]
 p_vect =3D [ start, end, end, start ]
 =20
 if( border ):
 #Close the loop
 p_vect.append(start)
 s_vect.append(0.0)
 if( self.vertical ):
 p =3D self.plot(s_vect, p_vect, 'b-', color=3D'black', =
linewidth=3D1.5)
 else:
 p =3D self.plot(p_vect, s_vect, 'b-', color=3D'black', =
linewidth=3D1.5)
 else:
 if( self.vertical ):
 p =3D self.fill(s_vect, p_vect, colour, linewidth=3D0.0, =
alpha=3D0.4)
 else:
 p =3D self.fill(p_vect, s_vect, colour, linewidth=3D0.0, =
alpha=3D0.4)
 def draw_needle( self ):
 if( self.raw_value =3D=3D None ):
 if( self.vertical ):
 self.text( (self.s_length + 0.05), self.graph_center, =
"N/A", size=3D10, va=3D'center', ha=3D'left')
 else:
 self.text( self.graph_center, (self.s_length + 0.05), =
"N/A", size=3D10, va=3D'bottom', ha=3D'center')
 else:
 #Clamp the value to the limits
 value =3D math.log10(self.raw_value)
 if( self.raw_value < self.raw_limits[0] ):
 value =3D math.log10(self.raw_limits[0])
 if( self.raw_value > self.raw_limits[1] ):
 value =3D math.log10(self.raw_limits[1])
 =20
 if( self.vertical ):
 self.text( (self.s_length + 0.05), value, "%.2f" % =
self.raw_value, size=3D10, va=3D'center', ha=3D'left')=20
 else:
 self.text( value, (self.s_length + 0.05), "%.2f" % =
self.raw_value, size=3D10, va=3D'bottom', ha=3D'center')=20
 =20
 if( not self.graph_positive ):
 value =3D -value
 s_vect =3D [ self.s_length/2, self.s_length, self.s_length ]
 p_vect =3D [ value + 0.00, value - 0.1, value + 0.1 ]
 =20
 if( self.vertical ):
 self.fill(s_vect, p_vect, 'black')
 else:
 self.fill(p_vect, s_vect, 'black')
 =20
 =20
 def draw_ticks( self ):
 i =3D self.raw_limits[0]
 step =3D self.raw_limits[0]
 while( i < self.raw_limits[1] ):
 while( i < (step * 10) ):
 value =3D math.log10(i)
 if( not self.graph_positive ):
 value =3D -value
 mantissa =3D int(i / math.pow(10, =
math.ceil(math.log10(i))-1))
 if( mantissa =3D=3D 10 or mantissa =3D=3D 1 ):
 if( self.vertical ):
 self.text( -0.05, value, "%g" % i, size=3D10, =
va=3D'center', ha=3D'right')
 else: =20
 self.text( value, -0.05, "%g" % i, size=3D10, =
va=3D'top', ha=3D'center')=20
 tick_length =3D self.s_length
 else:
 tick_length =3D self.s_length * 0.2
 s_vect =3D [ 0.0, tick_length ]
 p_vect =3D [ value, value ]
 if( self.vertical ):
 p =3D self.plot(s_vect, p_vect, 'b-', linewidth=3D1, =
color=3D'black', alpha=3D0.2)
 else:
 p =3D self.plot(p_vect, s_vect, 'b-', linewidth=3D1, =
color=3D'black', alpha=3D0.2)
 i +=3D step
 i =3D step * 10
 step =3D step * 10
 i =3D self.raw_limits[1]
 value =3D math.log10(i)
 if( not self.graph_positive ):
 value =3D -value
 mantissa =3D int(i / math.pow(10, math.ceil(math.log10(i))-1))
 if( mantissa =3D=3D 10 ):
 if( self.vertical ):
 self.text( -0.05, value, "%g" % i, size=3D10, =
va=3D'center', ha=3D'right')
 else: =20
 self.text( value, -0.05, "%g" % i, size=3D10, =
va=3D'top', ha=3D'center')=20
 =20
 =20
def make_widget( vertical, raw_value, raw_limits, raw_zones, =
attribute_name, field_names, file_name, resolution=3D72 ): =20
 from pylab import figure, show, savefig
 p_length =3D 2.0 # Length of the Primary axis
 s_length =3D 0.3 # Length of the Secondary axis
 =20
 if( vertical ):=20
 fig_height =3D p_length + 0.6
 fig_width =3D s_length + 1.0
 fig =3D figure( figsize=3D(fig_width, fig_height) )
 rect =3D [(0.5/fig_width), (0.4/fig_height), =
(s_length/fig_width), (p_length/fig_height)]
 meter =3D Meter(vertical, raw_value,=20
 raw_limits, raw_zones,=20
 attribute_name, field_names,=20
 file_name, resolution,=20
 p_length, s_length,
 fig, rect,
 xlim=3D( -0.2, s_length+0.1 ),
 ylim=3D( -0.1, p_length+0.1 ),
 xticks=3D[],
 yticks=3D[]
 )
 else:
 fig_height =3D s_length + 1.0
 fig_width =3D p_length + 0.4
 fig =3D figure( figsize=3D(fig_width, fig_height) )
 rect =3D [(0.2/fig_width), (0.5/fig_height), =
(p_length/fig_width), (s_length/fig_height)]
 meter =3D Meter(vertical, raw_value,=20
 raw_limits, raw_zones,=20
 attribute_name, field_names,=20
 file_name, resolution,
 p_length, s_length,
 fig, rect,
 xlim=3D( -0.1, p_length+0.1 ),
 ylim=3D( -0.4, s_length+0.1 ),
 xticks=3D[],
 yticks=3D[],
 )
 =20
 meter.set_axis_off()
 fig.add_axes(meter)
 # show()
 fig.canvas.print_figure( file_name,dpi=3Dresolution ) =20
 =20
 =20
#make_widget( True, 0.01, [0.001,10.0], =
[[0.001,0.01,'r'],[0.01,0.1,'y'],[0.1,10.0,'g']], "Rx MOS (24h)", ['WLL =
to LAS','LAS to WLL','WLL to LAS','LAS to WLL'], 'log_meter.png', 100)
 =20
''' =20
 =20
if __name__=3D=3D'__main__':
 from pylab import figure, show, savefig
 =20
 vertical =3D True =20
 =20
 raw_value =3D 0.1
 raw_limits =3D [0.0001,10.0,5,1]
 raw_zones =3D [[0.0001,0.001,'r'],[0.001,0.1,'y'],[0.1,10.0,'g']]
 attribute_name =3D "Rx MOS (24h)"
 =20
 p_length =3D 2.0 # Length of the Primary axis
 s_length =3D 0.3 # Length of the Secondary axis
 =20
 if( vertical ):=20
 fig_height =3D p_length + 0.6
 fig_width =3D s_length + 1.0
 fig =3D figure( figsize=3D(fig_width, fig_height) )
 rect =3D [(0.5/fig_width), (0.4/fig_height), =
(s_length/fig_width), (p_length/fig_height)]
 meter =3D Meter(fig, rect,
 xlim=3D( -0.2, s_length+0.1 ),
 ylim=3D( -0.1, p_length+0.1 ),
 xticks=3D[],
 yticks=3D[],
 )
 else:
 fig_height =3D s_length + 1.0
 fig_width =3D p_length + 0.4
 fig =3D figure( figsize=3D(fig_width, fig_height) )
 rect =3D [(0.2/fig_width), (0.5/fig_height), =
(p_length/fig_width), (s_length/fig_height)]
 meter =3D Meter(fig, rect,
 xlim=3D( -0.1, p_length+0.1 ),
 ylim=3D( -0.4, s_length+0.1 ),
 xticks=3D[],
 yticks=3D[],
 )
 =20
 meter.set_axis_off()
 fig.add_axes(meter)
 # show()
 fig.canvas.print_figure('log_meter',dpi=3D72)
'''
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.
Thanks for helping keep SourceForge clean.
X





Briefly describe the problem (required):
Upload screenshot of ad (required):
Select a file, or drag & drop file here.
Screenshot instructions:

Click URL instructions:
Right-click on the ad, choose "Copy Link", then paste here →
(This may not be possible with some types of ads)

More information about our ad policies

Ad destination/click URL:

AltStyle によって変換されたページ (->オリジナル) /