~ubuntu-branches/ubuntu/trusty/python-gnuplot/trusty

« back to all changes in this revision

Viewing changes to build/lib/Gnuplot/demo.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2002-04-11 08:52:48 UTC
  • Revision ID: james.westby@ubuntu.com-20020411085248-lq0l4xzelis5waq8
Tags: 1.5-3
Build-depend on python-numeric (closes: #142080).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
# $Id: demo.py,v 2.7 2001/01/07 21:35:12 mhagger Exp $
 
3
 
 
4
# Copyright (C) 1999-2001 Michael Haggerty <mhagger@alum.mit.edu>
 
5
#
 
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.
 
16
 
 
17
"""demo.py -- Demonstrate the Gnuplot python module.
 
18
 
 
19
Run this demo by typing 'python demo.py'.  For a more complete test of
 
20
the Gnuplot package, see test.py.
 
21
 
 
22
"""
 
23
 
 
24
__cvs_version__ = '$Revision: 2.7 $'
 
25
 
 
26
 
 
27
try:
 
28
    # If the package has been installed correctly, this should work:
 
29
    import Gnuplot, Gnuplot.funcutils
 
30
except ImportError:
 
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:
 
34
    import __init__
 
35
    Gnuplot = __init__
 
36
    import funcutils
 
37
    Gnuplot.funcutils = funcutils
 
38
 
 
39
 
 
40
def demo():
 
41
    """Demonstrate the Gnuplot package."""
 
42
 
 
43
    from Numeric import *
 
44
 
 
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
 
52
    # also be OK):
 
53
    g.plot([[0,1.1], [1,5.8], [2,3.3], [3,4.2]])
 
54
    raw_input('Please press return to continue...\n')
 
55
 
 
56
    g.reset()
 
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)
 
60
    y1 = x**2
 
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',
 
67
                     with='points 3 3')
 
68
    g.title('Data can be computed by python or gnuplot')
 
69
    g.xlabel('x')
 
70
    g.ylabel('x squared')
 
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')
 
74
 
 
75
    # Save what we just plotted as a color postscript file.
 
76
 
 
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')
 
85
 
 
86
    g.reset()
 
87
    # Demonstrate a 3-d plot:
 
88
    # set up x and y values at which the function will be tabulated:
 
89
    x = arange(35)/2.0
 
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:
 
94
    xm = x[:,NewAxis]
 
95
    ym = y[NewAxis,:]
 
96
    m = (sin(xm) + 0.1*xm) - ym**2
 
97
    g('set parametric')
 
98
    g('set data style lines')
 
99
    g('set hidden')
 
100
    g('set contour base')
 
101
    g.title('An example of a surface plot')
 
102
    g.xlabel('x')
 
103
    g.ylabel('y')
 
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')
 
113
 
 
114
    # plot another function, but letting GridFunc tabulate its values
 
115
    # automatically.  f could also be a lambda or a global function:
 
116
    def f(x,y):
 
117
        return 1.0 / (1 + 0.01 * x**2 + 0.5 * y**2)
 
118
 
 
119
    g.splot(Gnuplot.funcutils.compute_GridData(x,y, f, binary=0))
 
120
    raw_input('Please press return to continue...\n')
 
121
 
 
122
    # Explicit delete shouldn't be necessary, but if you are having
 
123
    # trouble with temporary files being left behind, try uncommenting
 
124
    # the following:
 
125
    #del g, d
 
126
 
 
127
 
 
128
# when executed, just run demo():
 
129
if __name__ == '__main__':
 
130
    demo()
 
131