Package PyFoam :: Package ThirdParty :: Package Gnuplot :: Module test
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.ThirdParty.Gnuplot.test

  1  #! /usr/bin/env python 
  2   
  3  # $Id: test.py,v 2.34 2003/08/18 22:33:00 mhagger Exp $ 
  4   
  5  # Copyright (C) 1999-2003 Michael Haggerty <mhagger@alum.mit.edu> 
  6  # 
  7  # This file is licensed under the GNU Lesser General Public License 
  8  # (LGPL).  See LICENSE.txt for details. 
  9   
 10  """test.py -- Exercise the Gnuplot.py module. 
 11   
 12  This module is not meant to be a flashy demonstration; rather it is a 
 13  thorough test of many combinations of Gnuplot.py features. 
 14   
 15  """ 
 16   
 17  __cvs_version__ = '$Revision: 2.34 $' 
 18   
 19  import os, time, math, tempfile 
 20  import Numeric 
 21  from Numeric import NewAxis 
 22   
 23  try: 
 24      import Gnuplot, Gnuplot.PlotItems, Gnuplot.funcutils 
 25  except ImportError: 
 26      # kludge in case Gnuplot hasn't been installed as a module yet: 
 27      import __init__ 
 28      Gnuplot = __init__ 
 29      import PlotItems 
 30      Gnuplot.PlotItems = PlotItems 
 31      import funcutils 
 32      Gnuplot.funcutils = funcutils 
 33   
 34   
35 -def wait(str=None, prompt='Press return to show results...\n'):
36 if str is not None: 37 print str 38 raw_input(prompt)
39 40
41 -def main():
42 """Exercise the Gnuplot module.""" 43 44 print ( 45 'This program exercises many of the features of Gnuplot.py. The\n' 46 'commands that are actually sent to gnuplot are printed for your\n' 47 'enjoyment.' 48 ) 49 50 wait('Popping up a blank gnuplot window on your screen.') 51 g = Gnuplot.Gnuplot(debug=1) 52 g.clear() 53 54 # Make a temporary file: 55 filename1 = tempfile.mktemp() 56 f = open(filename1, 'w') 57 try: 58 for x in Numeric.arange(100)/5. - 10.: 59 f.write('%s %s %s\n' % (x, math.cos(x), math.sin(x))) 60 f.close() 61 62 print '############### test Func ###################################' 63 wait('Plot a gnuplot-generated function') 64 g.plot(Gnuplot.Func('sin(x)')) 65 66 wait('Set title and axis labels and try replot()') 67 g.title('Title') 68 g.xlabel('x') 69 g.ylabel('y') 70 g.replot() 71 72 wait('Style linespoints') 73 g.plot(Gnuplot.Func('sin(x)', with='linespoints')) 74 wait('title=None') 75 g.plot(Gnuplot.Func('sin(x)', title=None)) 76 wait('title="Sine of x"') 77 g.plot(Gnuplot.Func('sin(x)', title='Sine of x')) 78 wait('axes=x2y2') 79 g.plot(Gnuplot.Func('sin(x)', axes='x2y2', title='Sine of x')) 80 81 print 'Change Func attributes after construction:' 82 f = Gnuplot.Func('sin(x)') 83 wait('Original') 84 g.plot(f) 85 wait('Style linespoints') 86 f.set_option(with='linespoints') 87 g.plot(f) 88 wait('title=None') 89 f.set_option(title=None) 90 g.plot(f) 91 wait('title="Sine of x"') 92 f.set_option(title='Sine of x') 93 g.plot(f) 94 wait('axes=x2y2') 95 f.set_option(axes='x2y2') 96 g.plot(f) 97 98 print '############### test File ###################################' 99 wait('Generate a File from a filename') 100 g.plot(Gnuplot.File(filename1)) 101 102 wait('Style lines') 103 g.plot(Gnuplot.File(filename1, with='lines')) 104 105 wait('using=1, using=(1,)') 106 g.plot(Gnuplot.File(filename1, using=1, with='lines'), 107 Gnuplot.File(filename1, using=(1,), with='points')) 108 wait('using=(1,2), using="1:3"') 109 g.plot(Gnuplot.File(filename1, using=(1,2)), 110 Gnuplot.File(filename1, using='1:3')) 111 112 wait('every=5, every=(5,)') 113 g.plot(Gnuplot.File(filename1, every=5, with='lines'), 114 Gnuplot.File(filename1, every=(5,), with='points')) 115 wait('every=(10,None,0), every="10::5"') 116 g.plot(Gnuplot.File(filename1, with='lines'), 117 Gnuplot.File(filename1, every=(10,None,0)), 118 Gnuplot.File(filename1, every='10::5')) 119 120 wait('title=None') 121 g.plot(Gnuplot.File(filename1, title=None)) 122 wait('title="title"') 123 g.plot(Gnuplot.File(filename1, title='title')) 124 125 print 'Change File attributes after construction:' 126 f = Gnuplot.File(filename1) 127 wait('Original') 128 g.plot(f) 129 wait('Style linespoints') 130 f.set_option(with='linespoints') 131 g.plot(f) 132 wait('using=(1,3)') 133 f.set_option(using=(1,3)) 134 g.plot(f) 135 wait('title=None') 136 f.set_option(title=None) 137 g.plot(f) 138 139 print '############### test Data ###################################' 140 x = Numeric.arange(100)/5. - 10. 141 y1 = Numeric.cos(x) 142 y2 = Numeric.sin(x) 143 d = Numeric.transpose((x,y1,y2)) 144 145 wait('Plot Data against its index') 146 g.plot(Gnuplot.Data(y2, inline=0)) 147 148 wait('Plot Data, specified column-by-column') 149 g.plot(Gnuplot.Data(x,y2, inline=0)) 150 wait('Same thing, inline data') 151 g.plot(Gnuplot.Data(x,y2, inline=1)) 152 153 wait('Plot Data, specified by an array') 154 g.plot(Gnuplot.Data(d, inline=0)) 155 wait('Same thing, inline data') 156 g.plot(Gnuplot.Data(d, inline=1)) 157 wait('with="lp 4 4"') 158 g.plot(Gnuplot.Data(d, with='lp 4 4')) 159 wait('cols=0') 160 g.plot(Gnuplot.Data(d, cols=0)) 161 wait('cols=(0,1), cols=(0,2)') 162 g.plot(Gnuplot.Data(d, cols=(0,1), inline=0), 163 Gnuplot.Data(d, cols=(0,2), inline=0)) 164 wait('Same thing, inline data') 165 g.plot(Gnuplot.Data(d, cols=(0,1), inline=1), 166 Gnuplot.Data(d, cols=(0,2), inline=1)) 167 wait('Change title and replot()') 168 g.title('New title') 169 g.replot() 170 wait('title=None') 171 g.plot(Gnuplot.Data(d, title=None)) 172 wait('title="Cosine of x"') 173 g.plot(Gnuplot.Data(d, title='Cosine of x')) 174 175 print '############### test compute_Data ###########################' 176 x = Numeric.arange(100)/5. - 10. 177 178 wait('Plot Data, computed by Gnuplot.py') 179 g.plot(Gnuplot.funcutils.compute_Data(x, lambda x: math.cos(x), inline=0)) 180 wait('Same thing, inline data') 181 g.plot(Gnuplot.funcutils.compute_Data(x, math.cos, inline=1)) 182 wait('with="lp 4 4"') 183 g.plot(Gnuplot.funcutils.compute_Data(x, math.cos, with='lp 4 4')) 184 185 print '############### test hardcopy ###############################' 186 print '******** Generating postscript file "gp_test.ps" ********' 187 wait() 188 g.plot(Gnuplot.Func('cos(0.5*x*x)', with='linespoints 2 2', 189 title='cos(0.5*x^2)')) 190 g.hardcopy('gp_test.ps') 191 192 wait('Testing hardcopy options: mode="eps"') 193 g.hardcopy('gp_test.ps', mode='eps') 194 wait('Testing hardcopy options: mode="landscape"') 195 g.hardcopy('gp_test.ps', mode='landscape') 196 wait('Testing hardcopy options: mode="portrait"') 197 g.hardcopy('gp_test.ps', mode='portrait') 198 wait('Testing hardcopy options: eps=1') 199 g.hardcopy('gp_test.ps', eps=1) 200 wait('Testing hardcopy options: mode="default"') 201 g.hardcopy('gp_test.ps', mode='default') 202 wait('Testing hardcopy options: enhanced=1') 203 g.hardcopy('gp_test.ps', enhanced=1) 204 wait('Testing hardcopy options: enhanced=0') 205 g.hardcopy('gp_test.ps', enhanced=0) 206 wait('Testing hardcopy options: color=1') 207 g.hardcopy('gp_test.ps', color=1) 208 # For some reason, 209 # g.hardcopy('gp_test.ps', color=0, solid=1) 210 # doesn't work here (it doesn't activate the solid option), even 211 # though the command sent to gnuplot looks correct. I'll 212 # tentatively conclude that it is a gnuplot bug. ### 213 wait('Testing hardcopy options: color=0') 214 g.hardcopy('gp_test.ps', color=0) 215 wait('Testing hardcopy options: solid=1') 216 g.hardcopy('gp_test.ps', solid=1) 217 wait('Testing hardcopy options: duplexing="duplex"') 218 g.hardcopy('gp_test.ps', solid=0, duplexing='duplex') 219 wait('Testing hardcopy options: duplexing="defaultplex"') 220 g.hardcopy('gp_test.ps', duplexing='defaultplex') 221 wait('Testing hardcopy options: fontname="Times-Italic"') 222 g.hardcopy('gp_test.ps', fontname='Times-Italic') 223 wait('Testing hardcopy options: fontsize=20') 224 g.hardcopy('gp_test.ps', fontsize=20) 225 226 print '############### test shortcuts ##############################' 227 wait('plot Func and Data using shortcuts') 228 g.plot('sin(x)', d) 229 230 print '############### test splot ##################################' 231 wait('a 3-d curve') 232 g.splot(Gnuplot.Data(d, with='linesp', inline=0)) 233 wait('Same thing, inline data') 234 g.splot(Gnuplot.Data(d, with='linesp', inline=1)) 235 236 print '############### test GridData and compute_GridData ##########' 237 # set up x and y values at which the function will be tabulated: 238 x = Numeric.arange(35)/2.0 239 y = Numeric.arange(30)/10.0 - 1.5 240 # Make a 2-d array containing a function of x and y. First create 241 # xm and ym which contain the x and y values in a matrix form that 242 # can be `broadcast' into a matrix of the appropriate shape: 243 xm = x[:,NewAxis] 244 ym = y[NewAxis,:] 245 m = (Numeric.sin(xm) + 0.1*xm) - ym**2 246 wait('a function of two variables from a GridData file') 247 g('set parametric') 248 g('set data style lines') 249 g('set hidden') 250 g('set contour base') 251 g.xlabel('x') 252 g.ylabel('y') 253 g.splot(Gnuplot.GridData(m,x,y, binary=0, inline=0)) 254 wait('Same thing, inline data') 255 g.splot(Gnuplot.GridData(m,x,y, binary=0, inline=1)) 256 257 wait('The same thing using binary mode') 258 g.splot(Gnuplot.GridData(m,x,y, binary=1)) 259 260 wait('The same thing using compute_GridData to tabulate function') 261 g.splot(Gnuplot.funcutils.compute_GridData( 262 x,y, lambda x,y: math.sin(x) + 0.1*x - y**2, 263 )) 264 265 wait('Use compute_GridData in ufunc and binary mode') 266 g.splot(Gnuplot.funcutils.compute_GridData( 267 x,y, lambda x,y: Numeric.sin(x) + 0.1*x - y**2, 268 ufunc=1, binary=1, 269 )) 270 271 wait('And now rotate it a bit') 272 for view in range(35,70,5): 273 g('set view 60, %d' % view) 274 g.replot() 275 time.sleep(1.0) 276 277 wait(prompt='Press return to end the test.\n') 278 finally: 279 os.unlink(filename1)
280 281 282 # when executed, just run main(): 283 if __name__ == '__main__': 284 main() 285