Home | Trees | Indices | Help |
---|
|
1 #! /usr/bin/env python 2 # $Id: demo.py,v 2.10 2003/04/21 09:44:09 mhagger Exp $ 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 __cvs_version__ = '$Revision: 2.10 $' 17 18 19 from Numeric import * 20 21 # If the package has been installed correctly, this should work: 22 import Gnuplot, Gnuplot.funcutils 23 2426 """Demonstrate the Gnuplot package.""" 27 28 # A straightforward use of gnuplot. The `debug=1' switch is used 29 # in these examples so that the commands that are sent to gnuplot 30 # are also output on stderr. 31 g = Gnuplot.Gnuplot(debug=1) 32 g.title('A simple example') # (optional) 33 g('set data style linespoints') # give gnuplot an arbitrary command 34 # Plot a list of (x, y) pairs (tuples or a Numeric array would 35 # also be OK): 36 g.plot([[0,1.1], [1,5.8], [2,3.3], [3,4.2]]) 37 raw_input('Please press return to continue...\n') 38 39 g.reset() 40 # Plot one dataset from an array and one via a gnuplot function; 41 # also demonstrate the use of item-specific options: 42 x = arange(10, typecode=Float) 43 y1 = x**2 44 # Notice how this plotitem is created here but used later? This 45 # is convenient if the same dataset has to be plotted multiple 46 # times. It is also more efficient because the data need only be 47 # written to a temporary file once. 48 d = Gnuplot.Data(x, y1, 49 title='calculated by python', 50 with='points 3 3') 51 g.title('Data can be computed by python or gnuplot') 52 g.xlabel('x') 53 g.ylabel('x squared') 54 # Plot a function alongside the Data PlotItem defined above: 55 g.plot(Gnuplot.Func('x**2', title='calculated by gnuplot'), d) 56 raw_input('Please press return to continue...\n') 57 58 # Save what we just plotted as a color postscript file. 59 60 # With the enhanced postscript option, it is possible to show `x 61 # squared' with a superscript (plus much, much more; see `help set 62 # term postscript' in the gnuplot docs). If your gnuplot doesn't 63 # support enhanced mode, set `enhanced=0' below. 64 g.ylabel('x^2') # take advantage of enhanced postscript mode 65 g.hardcopy('gp_test.ps', enhanced=1, color=1) 66 print ('\n******** Saved plot to postscript file "gp_test.ps" ********\n') 67 raw_input('Please press return to continue...\n') 68 69 g.reset() 70 # Demonstrate a 3-d plot: 71 # set up x and y values at which the function will be tabulated: 72 x = arange(35)/2.0 73 y = arange(30)/10.0 - 1.5 74 # Make a 2-d array containing a function of x and y. First create 75 # xm and ym which contain the x and y values in a matrix form that 76 # can be `broadcast' into a matrix of the appropriate shape: 77 xm = x[:,NewAxis] 78 ym = y[NewAxis,:] 79 m = (sin(xm) + 0.1*xm) - ym**2 80 g('set parametric') 81 g('set data style lines') 82 g('set hidden') 83 g('set contour base') 84 g.title('An example of a surface plot') 85 g.xlabel('x') 86 g.ylabel('y') 87 # The `binary=1' option would cause communication with gnuplot to 88 # be in binary format, which is considerably faster and uses less 89 # disk space. (This only works with the splot command due to 90 # limitations of gnuplot.) `binary=1' is the default, but here we 91 # disable binary because older versions of gnuplot don't allow 92 # binary data. Change this to `binary=1' (or omit the binary 93 # option) to get the advantage of binary format. 94 g.splot(Gnuplot.GridData(m,x,y, binary=0)) 95 raw_input('Please press return to continue...\n') 96 97 # plot another function, but letting GridFunc tabulate its values 98 # automatically. f could also be a lambda or a global function: 99 def f(x,y): 100 return 1.0 / (1 + 0.01 * x**2 + 0.5 * y**2)101 102 g.splot(Gnuplot.funcutils.compute_GridData(x,y, f, binary=0)) 103 raw_input('Please press return to continue...\n') 104 105 # Explicit delete shouldn't be necessary, but if you are having 106 # trouble with temporary files being left behind, try uncommenting 107 # the following: 108 #del g, d 109 110 111 # when executed, just run demo(): 112 if __name__ == '__main__': 113 demo() 114
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0alpha3 on Wed Aug 29 19:08:08 2007 | http://epydoc.sourceforge.net |