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