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