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