#======================================================================= # A complex example of a CDAT x-y line plot. # # Copyright (c) 2003 by Johnny Lin. For licensing, distribution # conditions, contact information, and additional documentation see # the URL http://www.johnny-lin.com/pylib.html#license. #======================================================================= #- Import modules: import Numeric as N import vcs #- Data: A sine curve: x = N.arange(17)*N.pi/8. y = N.sin(x) #- Open VCS canvas: v = vcs.init() #- Create x vs. y graphics method object and template: my_gm = v.createxvsy('my_graph_meth', 'default') my_tpl = v.createtemplate('my_template', 'default') #- Set plot symbol to square and linestyle to dashed: my_gm.marker = 11 my_gm.line = 1 #- Set coordinates (normalized units) for lower-left and upper- # right bounding box (bbll*, bbur*); set height of font for # the overall title (title_fontht), [xy]titles (xytitle_fontht), # and for the tick labels (ticklabel_fontht); set length of tick # marks (in normalized units): bbllx = 0.25 bblly = 0.25 bburx = 0.75 bbury = 0.75 title_fontht = 40 xytitle_fontht = 30 ticklabel_fontht = 30 ticklength = 0.01 #- Set font to use for the ticklabels (ticklabel_font) and the # titles (titles_font): ticklabel_font = 2 titles_font = 2 #- Set conversion factor to multiply font height coordinates by # to obtain the value in normalized coordinates. This is set # by trial-and-error: fontht2norm = 0.0007 #- Create text-table and text-orientation objects for x- and # y-axis tick labels and using them set font and font height. # Also set y-axis tick labels to be vertically aligned at the # mid-way point: ttab_xticklabel = v.createtexttable('xticklabel_ttab', 'default') ttab_yticklabel = v.createtexttable('yticklabel_ttab', 'default') tori_xticklabel = v.createtextorientation('xticklabel_tori', 'defcenter') tori_yticklabel = v.createtextorientation('yticklabel_tori', 'default') ttab_xticklabel.font = ticklabel_font ttab_yticklabel.font = ticklabel_font tori_xticklabel.height = ticklabel_fontht tori_yticklabel.height = ticklabel_fontht tori_yticklabel.valign = 'half' #- Create text-combined objects for overall title and axis # titles and set font, height, and content: text_title = v.createtext( 'title_ttab', 'default' \ , 'title_tori', 'defcenter' ) text_title.font = titles_font text_title.height = title_fontht text_title.string = 'A Nice Sine Curve' text_xtitle = v.createtext( 'xtitle_ttab', 'default' \ , 'xtitle_tori', 'defcenter' ) text_xtitle.font = titles_font text_xtitle.height = xytitle_fontht text_xtitle.string = 'x' text_ytitle = v.createtext( 'ytitle_ttab', 'default' \ , 'ytitle_tori', 'defcentup' ) text_ytitle.font = titles_font text_ytitle.height = xytitle_fontht text_ytitle.string = 'sin(x)' #- Turn off some default titling fields: my_tpl.xname.priority = 0 my_tpl.yname.priority = 0 my_tpl.mean.priority = 0 my_tpl.max.priority = 0 my_tpl.min.priority = 0 #- Set position of data field, plot box, and x-axis bottom ticks # and labels; set text-table and text-orientation of x-axis bottom # labels; turn off upper-right set of tick labels: my_tpl.data.x1 = my_tpl.box1.x1 = bbllx my_tpl.data.y1 = my_tpl.box1.y1 = bblly my_tpl.data.x2 = my_tpl.box1.x2 = bburx my_tpl.data.y2 = my_tpl.box1.y2 = bbury my_tpl.xtic1.y1 = bblly - ticklength my_tpl.xtic1.y2 = bblly my_tpl.xlabel1.y = bblly - (3.0 * ticklength) my_tpl.xlabel1.texttable = ttab_xticklabel my_tpl.xlabel1.textorientation = tori_xticklabel my_tpl.xtic2.priority = 0 my_tpl.ytic2.priority = 0 #- Calculate "nice" x-axis labels and set them into the graphics # method, with no more than 8 ticks on the axis: my_gm.xticlabels1 = vcs.mklabels( vcs.mkscale(min(x), max(x), 8) ) #- Calculate "nice" y-axis labels, set them in the graphics method, # calculate y-axis title location based on the longest label # length, set y-axis ticks and tick label locations, and set # text-table and text-orientation for y-axis tick labels: ylabels = vcs.mklabels( vcs.mkscale(min(y), max(y)) ) my_gm.yticlabels1 = ylabels longest_ylabels = \ max([ len(ylabels.values()[i]) for i in range(len(ylabels))] ) my_tpl.ytic1.x1 = bbllx - ticklength my_tpl.ytic1.x2 = bbllx my_tpl.ylabel1.x = bbllx - ticklength \ - ( longest_ylabels * ticklabel_fontht * fontht2norm ) my_tpl.ylabel1.texttable = ttab_yticklabel my_tpl.ylabel1.textorientation = tori_yticklabel #- Position overall title, x-axis title, and y-axis title: text_title.x = [ (bburx-bbllx)/2.0 + bbllx ] text_title.y = [ bbury + (1.7 * title_fontht * fontht2norm) ] text_xtitle.x = [ (bburx-bbllx)/2.0 + bbllx ] text_xtitle.y = [ my_tpl.xlabel1.y - (1.7 * title_fontht * fontht2norm) ] text_ytitle.x = [ my_tpl.ylabel1.x - ( 1.6 * title_fontht * fontht2norm ) ] text_ytitle.y = [ (bbury-bblly)/2.0 + bblly ] #- Render plot, plot overall title, x-axis title, and y-axis title: v.plot(x, y, my_gm, my_tpl, name=' ') v.plot(text_title) v.plot(text_xtitle) v.plot(text_ytitle) #- Postscript output of the plot: v.postscript('sine_plot.ps') v.gif('sine_plot.gif') #====== end of file ======