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

Source Code for Module PyFoam.ThirdParty.Gnuplot.demo

  1  #! /usr/bin/env python 
  2  # $Id: demo.py 299 2007-03-30 12:52:17Z mhagger $ 
  3   
  4  # Copyright (C) 1999-2003 Michael Haggerty <mhagger@alum.mit.edu> 
  5  # 
  6  # This file is licensed under the GNU Lesser General Public License 
  7  # (LGPL).  See LICENSE.txt for details. 
  8   
  9  """demo.py -- Demonstrate the Gnuplot python module. 
 10   
 11  Run this demo by typing 'python demo.py'.  For a more complete test of 
 12  the Gnuplot package, see test.py. 
 13   
 14  """ 
 15   
 16  from numpy import * 
 17   
 18  # If the package has been installed correctly, this should work: 
 19  from PyFoam.ThirdParty import Gnuplot 
 20  from PyFoam.ThirdParty.Gnuplot import funcutils 
 21   
 22  from PyFoam.ThirdParty.six.moves import input as rinput 
 23   
24 -def demo():
25 """Demonstrate the Gnuplot package.""" 26 27 # A straightforward use of gnuplot. The `debug=1' switch is used 28 # in these examples so that the commands that are sent to gnuplot 29 # are also output on stderr. 30 g = Gnuplot.Gnuplot(debug=1) 31 g.title('A simple example') # (optional) 32 g('set style data linespoints') # give gnuplot an arbitrary command 33 # Plot a list of (x, y) pairs (tuples or a numpy array would 34 # also be OK): 35 g.plot([[0,1.1], [1,5.8], [2,3.3], [3,4.2]]) 36 rinput('Please press return to continue...\n') 37 38 g.reset() 39 # Plot one dataset from an array and one via a gnuplot function; 40 # also demonstrate the use of item-specific options: 41 try: 42 x = arange(10, dtype='float_') 43 except TypeError: 44 x = arange(10, typecode='d') 45 46 y1 = x**2 47 # Notice how this plotitem is created here but used later? This 48 # is convenient if the same dataset has to be plotted multiple 49 # times. It is also more efficient because the data need only be 50 # written to a temporary file once. 51 d = Gnuplot.Data(x, y1, 52 title='calculated by python', 53 with_='points pt 3 ps 3') 54 g.title('Data can be computed by python or gnuplot') 55 g.xlabel('x') 56 g.ylabel('x squared') 57 # Plot a function alongside the Data PlotItem defined above: 58 g.plot(Gnuplot.Func('x**2', title='calculated by gnuplot'), d) 59 rinput('Please press return to continue...\n') 60 61 # Save what we just plotted as a color postscript file. 62 63 # With the enhanced postscript option, it is possible to show `x 64 # squared' with a superscript (plus much, much more; see `help set 65 # term postscript' in the gnuplot docs). If your gnuplot doesn't 66 # support enhanced mode, set `enhanced=0' below. 67 g.ylabel('x^2') # take advantage of enhanced postscript mode 68 g.hardcopy('gp_test.ps', enhanced=1, color=1) 69 print ('\n******** Saved plot to postscript file "gp_test.ps" ********\n') 70 rinput('Please press return to continue...\n') 71 72 g.reset() 73 # Demonstrate a 3-d plot: 74 # set up x and y values at which the function will be tabulated: 75 x = arange(35)/2.0 76 y = arange(30)/10.0 - 1.5 77 # Make a 2-d array containing a function of x and y. First create 78 # xm and ym which contain the x and y values in a matrix form that 79 # can be `broadcast' into a matrix of the appropriate shape: 80 xm = x[:,newaxis] 81 ym = y[newaxis,:] 82 m = (sin(xm) + 0.1*xm) - ym**2 83 g('set parametric') 84 g('set style data lines') 85 g('set hidden') 86 g('set contour base') 87 g.title('An example of a surface plot') 88 g.xlabel('x') 89 g.ylabel('y') 90 # The `binary=1' option would cause communication with gnuplot to 91 # be in binary format, which is considerably faster and uses less 92 # disk space. (This only works with the splot command due to 93 # limitations of gnuplot.) `binary=1' is the default, but here we 94 # disable binary because older versions of gnuplot don't allow 95 # binary data. Change this to `binary=1' (or omit the binary 96 # option) to get the advantage of binary format. 97 g.splot(Gnuplot.GridData(m,x,y, binary=0)) 98 rinput('Please press return to continue...\n') 99 100 # plot another function, but letting GridFunc tabulate its values 101 # automatically. f could also be a lambda or a global function: 102 def f(x,y): 103 return 1.0 / (1 + 0.01 * x**2 + 0.5 * y**2)
104 105 g.splot(Gnuplot.funcutils.compute_GridData(x,y, f, binary=0)) 106 rinput('Please press return to continue...\n') 107 108 # Explicit delete shouldn't be necessary, but if you are having 109 # trouble with temporary files being left behind, try uncommenting 110 # the following: 111 #del g, d 112 113 114 # when executed, just run demo(): 115 if __name__ == '__main__': 116 demo() 117 118 # Should work with Python3 and Python2 119