Home | Trees | Indices | Help |
---|
|
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 try: 17 from numpy import * 18 except ImportError: 19 from Numeric import * 20 newaxis=NewAxis 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 numpy 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 try: 43 x = arange(10, dtype='float_') 44 except TypeError: 45 x = arange(10, typecode='d') 46 47 y1 = x**2 48 # Notice how this plotitem is created here but used later? This 49 # is convenient if the same dataset has to be plotted multiple 50 # times. It is also more efficient because the data need only be 51 # written to a temporary file once. 52 d = Gnuplot.Data(x, y1, 53 title='calculated by python', 54 with_='points 3 3') 55 g.title('Data can be computed by python or gnuplot') 56 g.xlabel('x') 57 g.ylabel('x squared') 58 # Plot a function alongside the Data PlotItem defined above: 59 g.plot(Gnuplot.Func('x**2', title='calculated by gnuplot'), d) 60 raw_input('Please press return to continue...\n') 61 62 # Save what we just plotted as a color postscript file. 63 64 # With the enhanced postscript option, it is possible to show `x 65 # squared' with a superscript (plus much, much more; see `help set 66 # term postscript' in the gnuplot docs). If your gnuplot doesn't 67 # support enhanced mode, set `enhanced=0' below. 68 g.ylabel('x^2') # take advantage of enhanced postscript mode 69 g.hardcopy('gp_test.ps', enhanced=1, color=1) 70 print ('\n******** Saved plot to postscript file "gp_test.ps" ********\n') 71 raw_input('Please press return to continue...\n') 72 73 g.reset() 74 # Demonstrate a 3-d plot: 75 # set up x and y values at which the function will be tabulated: 76 x = arange(35)/2.0 77 y = arange(30)/10.0 - 1.5 78 # Make a 2-d array containing a function of x and y. First create 79 # xm and ym which contain the x and y values in a matrix form that 80 # can be `broadcast' into a matrix of the appropriate shape: 81 xm = x[:,newaxis] 82 ym = y[newaxis,:] 83 m = (sin(xm) + 0.1*xm) - ym**2 84 g('set parametric') 85 g('set data style lines') 86 g('set hidden') 87 g('set contour base') 88 g.title('An example of a surface plot') 89 g.xlabel('x') 90 g.ylabel('y') 91 # The `binary=1' option would cause communication with gnuplot to 92 # be in binary format, which is considerably faster and uses less 93 # disk space. (This only works with the splot command due to 94 # limitations of gnuplot.) `binary=1' is the default, but here we 95 # disable binary because older versions of gnuplot don't allow 96 # binary data. Change this to `binary=1' (or omit the binary 97 # option) to get the advantage of binary format. 98 g.splot(Gnuplot.GridData(m,x,y, binary=0)) 99 raw_input('Please press return to continue...\n') 100 101 # plot another function, but letting GridFunc tabulate its values 102 # automatically. f could also be a lambda or a global function: 103 def f(x,y): 104 return 1.0 / (1 + 0.01 * x**2 + 0.5 * y**2)105 106 g.splot(Gnuplot.funcutils.compute_GridData(x,y, f, binary=0)) 107 raw_input('Please press return to continue...\n') 108 109 # Explicit delete shouldn't be necessary, but if you are having 110 # trouble with temporary files being left behind, try uncommenting 111 # the following: 112 #del g, d 113 114 115 # when executed, just run demo(): 116 if __name__ == '__main__': 117 demo() 118
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Wed Jun 9 23:05:13 2010 | http://epydoc.sourceforge.net |