2
# $Id: demo.py,v 2.7 2001/01/07 21:35:12 mhagger Exp $
4
# Copyright (C) 1999-2001 Michael Haggerty <mhagger@alum.mit.edu>
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version. This program is distributed in
10
# the hope that it will be useful, but WITHOUT ANY WARRANTY; without
11
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
12
# PARTICULAR PURPOSE. See the GNU General Public License for more
13
# details; it is available at <http://www.fsf.org/copyleft/gpl.html>,
14
# or by writing to the Free Software Foundation, Inc., 59 Temple Place
15
# - Suite 330, Boston, MA 02111-1307, USA.
17
"""demo.py -- Demonstrate the Gnuplot python module.
19
Run this demo by typing 'python demo.py'. For a more complete test of
20
the Gnuplot package, see test.py.
24
__cvs_version__ = '$Revision: 2.7 $'
28
# If the package has been installed correctly, this should work:
29
import Gnuplot, Gnuplot.funcutils
31
# It may be that the user is just testing out the package by
32
# running 'python demo.py' it the package's directory. If that is
33
# the case, the following should work:
37
Gnuplot.funcutils = funcutils
41
"""Demonstrate the Gnuplot package."""
45
# A straightforward use of gnuplot. The `debug=1' switch is used
46
# in these examples so that the commands that are sent to gnuplot
47
# are also output on stderr.
48
g = Gnuplot.Gnuplot(debug=1)
49
g.title('A simple example') # (optional)
50
g('set data style linespoints') # give gnuplot an arbitrary command
51
# Plot a list of (x, y) pairs (tuples or a Numeric array would
53
g.plot([[0,1.1], [1,5.8], [2,3.3], [3,4.2]])
54
raw_input('Please press return to continue...\n')
57
# Plot one dataset from an array and one via a gnuplot function;
58
# also demonstrate the use of item-specific options:
59
x = arange(10, typecode=Float)
61
# Notice how this plotitem is created here but used later? This
62
# is convenient if the same dataset has to be plotted multiple
63
# times. It is also more efficient because the data need only be
64
# written to a temporary file once.
65
d = Gnuplot.Data(x, y1,
66
title='calculated by python',
68
g.title('Data can be computed by python or gnuplot')
71
# Plot a function alongside the Data PlotItem defined above:
72
g.plot(Gnuplot.Func('x**2', title='calculated by gnuplot'), d)
73
raw_input('Please press return to continue...\n')
75
# Save what we just plotted as a color postscript file.
77
# With the enhanced postscript option, it is possible to show `x
78
# squared' with a superscript (plus much, much more; see `help set
79
# term postscript' in the gnuplot docs). If your gnuplot doesn't
80
# support enhanced mode, set `enhanced=0' below.
81
g.ylabel('x^2') # take advantage of enhanced postscript mode
82
g.hardcopy('gp_test.ps', enhanced=1, color=1)
83
print ('\n******** Saved plot to postscript file "gp_test.ps" ********\n')
84
raw_input('Please press return to continue...\n')
87
# Demonstrate a 3-d plot:
88
# set up x and y values at which the function will be tabulated:
90
y = arange(30)/10.0 - 1.5
91
# Make a 2-d array containing a function of x and y. First create
92
# xm and ym which contain the x and y values in a matrix form that
93
# can be `broadcast' into a matrix of the appropriate shape:
96
m = (sin(xm) + 0.1*xm) - ym**2
98
g('set data style lines')
100
g('set contour base')
101
g.title('An example of a surface plot')
104
# The `binary=1' option would cause communication with gnuplot to
105
# be in binary format, which is considerably faster and uses less
106
# disk space. (This only works with the splot command due to
107
# limitations of gnuplot.) `binary=1' is the default, but here we
108
# disable binary because older versions of gnuplot don't allow
109
# binary data. Change this to `binary=1' (or omit the binary
110
# option) to get the advantage of binary format.
111
g.splot(Gnuplot.GridData(m,x,y, binary=0))
112
raw_input('Please press return to continue...\n')
114
# plot another function, but letting GridFunc tabulate its values
115
# automatically. f could also be a lambda or a global function:
117
return 1.0 / (1 + 0.01 * x**2 + 0.5 * y**2)
119
g.splot(Gnuplot.funcutils.compute_GridData(x,y, f, binary=0))
120
raw_input('Please press return to continue...\n')
122
# Explicit delete shouldn't be necessary, but if you are having
123
# trouble with temporary files being left behind, try uncommenting
128
# when executed, just run demo():
129
if __name__ == '__main__':