Text of discourse.matplotlib.org › t › gauges-again-source-code-this-time › 3074 as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Gauges again, source code this time. - matplotlib-devel - Matplotlib

Gauges again, source code this time. Development matplotlib-devel _Jamil_Khan July 14, 2005, 12:12am 1 Gauges again, source code this time. Apparently the mailing list doesn’t do attachments. So here is the source instead: Jamil Khan Design Engineer, Calyptech Pty. Ltd. www.calyptech.com ··· =========================================================================== linear rotary gauge =========================================================================== #!/usr/bin/env python This program draws a semi-circular gauge.

You supply limits, shaded regions, names and the current value, and invoke it like this: draw_widget( current_value, limits[ lower, upper, major ticks, minor ticks], shaded zones [ [lower,upper,colour], [lower,upper,colour], …], name “MOS”, filename “gauge_0.png”, resolution in dpi ) from pylab import * #from matplotlib.patches import Patch, Rectangle, Circle, Polygon, Wedge, Shadow, bbox_artist import math import types import matplotlib matplotlib.use(‘Agg’) # force the antigrain backend from matplotlib.backends.backend_agg import FigureCanvasAgg def draw_arch(limits, start_angle, end_angle, colour, border, graph_positive): x_vect = [] y_vect = [] if( graph_positive ): start = int(180 - (start_angle - limits[0]) * (180.0/(limits[1]-limits[0]))) end = int(180 - (end_angle - limits[0]) * (180.0/(limits[1]-limits[0]))) else: start = int( (end_angle - limits[0]) * (180.0/(limits[1]-limits[0]))) end = int( (start_angle - limits[0]) * (180.0/(limits[1]-limits[0]))) #Draw the arch theta = start radius = 0.85 while (theta >= end): x_vect.append( radius * math.cos(theta * (pi/180)) ) y_vect.append( radius * math.sin(theta * (pi/180)) ) theta -= 1 theta = end radius = 1.0 while (theta <= start): x_vect.append( radius * math.cos(theta * (pi/180)) ) y_vect.append( radius * math.sin(theta * (pi/180)) ) theta += 1 if( border ): #Close the loop x_vect.append(-0.85) y_vect.append(0.0) p = plot(x_vect, y_vect, 'b-') setp(p, color='black') setp(p, linewidth=1.5) else: p = fill(x_vect, y_vect, colour) setp(p, linewidth=0.0) setp(p, alpha=0.4) def draw_needle(current_value, limits, graph_positive): x_vect = [] y_vect = [] if current_value == None: text(0.0, 0.4, "N/A", size=10, va='bottom', ha='center') else: text(0.0, 0.4, "%.2f" % current_value, size=10, va='bottom', ha='center') #Clamp the value to the limits if( current_value < limits[0] ): current_value = limits[0] if( current_value > limits[1] ): current_value = limits[1] theta = 0 length = 0.95 if( graph_positive ): angle = 180.0 - (current_value - limits[0]) *(180.0/abs(limits[1]-limits[0])) else: angle = (current_value - limits[0]) *(180.0/abs(limits[1]-limits[0])) while (theta <= 270): x_vect.append( length * math.cos((theta + angle) * (pi/180)) ) y_vect.append( length * math.sin((theta + angle) * (pi/180)) ) length = 0.05 theta += 90 p = fill(x_vect, y_vect, 'b') setp(p, alpha=0.4) def draw_ticks(limits, graph_positive): if( graph_positive ): angle = 180.0 else: angle = 0.0 i = 0 j = limits[0] iterating = True while( i*limits[3] + limits[0] <= limits[1] ): x_vect = [] y_vect = [] if( i % (limits[2]/limits[3]) == 0 ): x_pos = 1.1 * math.cos( angle * (pi/180.0)) y_pos = 1.1 * math.sin( angle * (pi/180.0)) if( type(limits[2]) is types.FloatType ): text( x_pos, y_pos, "%.2f" % j, size=10, va='center', ha='center', rotation=(angle - 90)) else: text( x_pos, y_pos, "%d" % int(j), size=10, va='center', ha='center', rotation=(angle - 90)) tick_length = 0.15 j += limits[2] else: tick_length = 0.05 i += 1 x_vect.append( 1.0 * math.cos( angle * (pi/180.0))) x_vect.append( (1.0 - tick_length) * math.cos( angle * (pi/180.0))) y_vect.append( 1.0 * math.sin( angle * (pi/180.0))) y_vect.append( (1.0 - tick_length) * math.sin( angle * (pi/180.0))) p = plot(x_vect, y_vect, 'b-') setp(p, linewidth=1) setp(p, alpha=0.4) setp(p, color="black") if( graph_positive ): angle -= limits[3] * (180.0/abs(limits[1]-limits[0])) else: angle += limits[3] * (180.0/abs(limits[1]-limits[0])) if( i % (limits[2]/limits[3]) == 0 ): x_pos = 1.1 * math.cos( angle * (pi/180.0)) y_pos = 1.1 * math.sin( angle * (pi/180.0)) if( type(limits[2]) is types.FloatType ): text( x_pos, y_pos, "%.2f" % j, size=10, va='center', ha='center', rotation=(angle - 90)) else: text( x_pos, y_pos, "%d" % int(j), size=10, va='center', ha='center', rotation=(angle - 90)) def draw_bounding_box(graph_width, graph_height): x_vect = [] y_vect = [] x_vect.append( (graph_width/2) ) x_vect.append( (graph_width/2) ) x_vect.append(-(graph_width/2) ) x_vect.append(-(graph_width/2) ) x_vect.append( (graph_width/2) ) y_vect.append(-0.1) y_vect.append(graph_height) y_vect.append(graph_height) y_vect.append(-0.1) y_vect.append(-0.1) p = plot(x_vect, y_vect, 'r-') setp(p, linewidth=0) def draw_widget( current_value, limits, zone_colour, attribute_name, filename, resolution=72 ): graph_height = 1.6 graph_width = 2.4 fig_height = graph_height fig_width = graph_width figure(figsize=(fig_width, fig_height )) a = axes([(0.0/fig_width), (0.2/fig_height), (graph_width/fig_width), (graph_height/fig_height)]) setp(a, xlim=( -0.1, graph_width+0.1 ), ylim=( -0.4, graph_height+0.1 ), xticks=[], yticks=[]) axis('off') #Perform Checking if( limits[0] == limits[1] ): raise 'identical_limits_exception', limits if( limits[1] > limits[0] ): graph_positive = True else: #Swap the limits around graph_positive = False temp = limits[0] limits[0] = limits[1] limits[1] = temp if not( ((limits[2]/limits[3]) % 1.0) * limits[3] == 0 ): #There must be an integer number of minor ticks for each major tick raise 'bad_tick_spacing_exception' if( limits[2] <= 0 or limits[3] <= 0 or limits[2] < limits[3] or limits[3] > abs(limits[1]-limits[0]) ): raise 'bad_limits_exception', limits for zone in zone_colour: if( zone[0] > zone[1] ): #Swap the zones so zone[1] > zone[0] temp = zone[0] zone[0] = zone[1] zone[1] = temp if( zone[1] < limits[0] or zone[0] > limits[1] ): raise 'bad_zone_exception', zone if( zone[0] < limits[0] ): zone[0] = limits[0] if( zone[1] > limits[1] ): zone[1] = limits[1] #Draw the arch for zone in zone_colour: draw_arch(limits, zone[0], zone[1], zone[2], False, graph_positive) draw_arch(limits, limits[0], limits[1], 'black', True, graph_positive) draw_ticks(limits, graph_positive) draw_needle(current_value, limits, graph_positive) draw_bounding_box(graph_width, graph_height) text(0.0, 0.2, attribute_name, size=10, va='bottom', ha='center') #The black dot. p = plot([0.0],[0.0],'.') setp(p, color='#000000') savefig( filename, dpi=resolution) #draw_widget ( 21.0, [4.0,20.0,4,1.0], [[4.0,10.0,‘r’],[10.0,14.0,‘y’],[14.0,20.0,‘g’]], “Rx MOS (24h)”, “gauge_0.png”, 100) #draw_widget ( -10.0, [-20.0,-4,4.0,1.0], [[-4.0,-10.0,‘r’],[-10.0,-14.0,‘y’],[-14.0,-20.0,‘g’]], “Rx MOS (24h)”, “gauge_0.png”, 100) #draw_widget ( -10.5, [20.0,-20.0,4.0,1.0], [[-20.0,-15.0,‘r’],[-10.0,-15.0,‘y’],[-10.0,20.0,‘g’]], “Rx MOS (24h)”, “gauge_0.png”, 100) #draw_widget ( None, [4.0,20.0,4,1.0], [[4.0,10.0,‘r’],[10.0,14.0,‘y’],[14.0,20.0,‘g’]], “Rx MOS (24h)”, “gauge_0.png”, 100) #draw_widget ( 1.2, [4.0,10.0,2,0.5], [[4.0,6.0,‘r’],[6.0,8.0,‘y’],[8.0,10.0,‘g’]], “Rx MOS (24h)”, “gauge_1.png”, 100) draw_widget( -4.0, [-1.0,1.0,1,0.1], [[-1.0,0.0,‘r’],[0.0,0.5,‘y’],[0.5,1.0,‘g’]], “Rx MOS (24h)”, “gauge_2.png”, 100) #show () =========================================================================== linear horizontal gauge =========================================================================== #!/usr/bin/env python This program draws a horizontal linear meter.

You supply limits, shaded regions, names and the current value, and invoke it like this: draw_widget( current_value, limits[ lower, upper, major ticks, minor ticks], shaded zones [ [lower,upper,colour], [lower,upper,colour], …], name “MOS”, filename “v_meter_0.png”, resolution in dpi ) from pylab import * impor

The rest of this page is on the live site.