File: test.py

package info (click to toggle)
python-gnuplot 1.4-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 364 kB
  • ctags: 172
  • sloc: python: 992; makefile: 35; sh: 26
file content (243 lines) | stat: -rwxr-xr-x 8,214 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#! /usr/bin/env python
# $Id: test.py,v 2.20 1999/10/14 03:16:16 mhagger Exp $

"""test.py -- Exercise the Gnuplot.py module.

This module is not meant to be a flashy demonstration; rather it is a
thorough test of many combinations of Gnuplot.py features.

Copyright (C) 1999 Michael Haggerty

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.  This program is distributed in the
hope that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the GNU General Public License for more details; it is
available at <http://www.fsf.org/copyleft/gpl.html>, or by writing to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.

"""

__version__ = '1.4'
__cvs_version__ = '$Revision: 2.20 $'

import math, time
import Numeric
from Numeric import NewAxis

try:
    import Gnuplot
except:
    # kludge in case Gnuplot hasn't been installed as a module yet:
    import __init__
    Gnuplot = __init__

gp = Gnuplot # abbreviation

def wait(str=None, prompt='Press return to show results...\n'):
    if str is not None:
        print str
    raw_input(prompt)


def main():
    """Exercise the Gnuplot module."""

    wait('Popping up a blank gnuplot window on your screen.')
    g = gp.Gnuplot()
    g.clear()

    # Make a temporary file:
    file1 = gp.TempFile() # will be deleted upon exit
    f = open(file1.filename, 'w')
    for x in Numeric.arange(100)/5. - 10.:
        f.write('%s %s %s\n' % (x, math.cos(x), math.sin(x)))
    f.close()

    print '############### test Func ########################################'
    wait('Plot a gnuplot-generated function')
    g.plot(gp.Func('sin(x)'))

    wait('Set title and axis labels and try replot()')
    g.title('Title')
    g.xlabel('x')
    g.ylabel('y')
    g.replot()

    wait('Style linespoints')
    g.plot(gp.Func('sin(x)', with='linespoints'))
    wait('title=None')
    g.plot(gp.Func('sin(x)', title=None))
    wait('title="Sine of x"')
    g.plot(gp.Func('sin(x)', title='Sine of x'))

    print 'Change Func attributes after construction:'
    f = gp.Func('sin(x)')
    wait('Original')
    g.plot(f)
    wait('Style linespoints')
    f.set_option(with='linespoints')
    g.plot(f)
    wait('title=None')
    f.set_option(title=None)
    g.plot(f)
    wait('title="Sine of x"')
    f.set_option(title='Sine of x')
    g.plot(f)

    print '############### test File ########################################'
    wait('Generate a File from a filename')
    g.plot(gp.File(file1.filename))
    wait('Generate a File given a TempFile object')
    g.plot(gp.File(file1))

    wait('Style lines')
    g.plot(gp.File(file1.filename, with='lines'))
    wait('using=1, using=(1,)')
    g.plot(gp.File(file1.filename, using=1, with='lines'),
           gp.File(file1.filename, using=(1,), with='points'))
    wait('using=(1,2), using="1:3"')
    g.plot(gp.File(file1.filename, using=(1,2)),
           gp.File(file1.filename, using='1:3'))
    wait('title=None')
    g.plot(gp.File(file1.filename, title=None))
    wait('title="title"')
    g.plot(gp.File(file1.filename, title='title'))

    print 'Change File attributes after construction:'
    f = gp.File(file1.filename)
    wait('Original')
    g.plot(f)
    wait('Style linespoints')
    f.set_option(with='linespoints')
    g.plot(f)
    wait('using=(1,3)')
    f.set_option(using=(1,3))
    g.plot(f)
    wait('title=None')
    f.set_option(title=None)
    g.plot(f)

    print '############### test Data ########################################'
    x = Numeric.arange(100)/5. - 10.
    y1 = Numeric.cos(x)
    y2 = Numeric.sin(x)
    d = Numeric.transpose((x,y1,y2))

    wait('Plot Data, specified column-by-column')
    g.plot(gp.Data(x,y2, inline=0))
    wait('Same thing, inline data')
    g.plot(gp.Data(x,y2, inline=1))

    wait('Plot Data, specified by an array')
    g.plot(gp.Data(d, inline=0))
    wait('Same thing, inline data')
    g.plot(gp.Data(d, inline=1))
    wait('with="lp 4 4"')
    g.plot(gp.Data(d, with='lp 4 4'))
    wait('cols=0')
    g.plot(gp.Data(d, cols=0))
    wait('cols=(0,1), cols=(0,2)')
    g.plot(gp.Data(d, cols=(0,1), inline=0),
           gp.Data(d, cols=(0,2), inline=0))
    wait('Same thing, inline data')
    g.plot(gp.Data(d, cols=(0,1), inline=1),
           gp.Data(d, cols=(0,2), inline=1))
    wait('Change title and replot()')
    g.title('New title')
    g.replot()
    wait('title=None')
    g.plot(gp.Data(d, title=None))
    wait('title="Cosine of x"')
    g.plot(gp.Data(d, title='Cosine of x'))

    print '############### test hardcopy ####################################'
    print '******** Generating postscript file "gp_test.ps" ********'
    wait()
    g.plot(gp.Func('cos(0.5*x*x)', with='linespoints 2 2',
                   title='cos(0.5*x^2)'))
    g.hardcopy('gp_test.ps')

    wait('Testing hardcopy options: mode="landscape"')
    g.hardcopy('gp_test.ps', mode='landscape')
    wait('Testing hardcopy options: mode="portrait"')
    g.hardcopy('gp_test.ps', mode='portrait')
    wait('Testing hardcopy options: mode="eps"')
    g.hardcopy('gp_test.ps', mode='eps')
    wait('Testing hardcopy options: eps=1')
    g.hardcopy('gp_test.ps', eps=1)
    wait('Testing hardcopy options: enhanced=1')
    g.hardcopy('gp_test.ps', eps=0, enhanced=1)
    wait('Testing hardcopy options: enhanced=0')
    g.hardcopy('gp_test.ps', enhanced=0)
    wait('Testing hardcopy options: color=1')
    g.hardcopy('gp_test.ps', color=1)
    wait('Testing hardcopy options: solid=1')
    g.hardcopy('gp_test.ps', color=0, solid=1)
    wait('Testing hardcopy options: duplexing="duplex"')
    g.hardcopy('gp_test.ps', solid=0, duplexing='duplex')
    wait('Testing hardcopy options: duplexing="defaultplex"')
    g.hardcopy('gp_test.ps', duplexing='defaultplex')
    wait('Testing hardcopy options: fontname="Times-Italic"')
    g.hardcopy('gp_test.ps', fontname='Times-Italic')
    wait('Testing hardcopy options: fontsize=20')
    g.hardcopy('gp_test.ps', fontsize=20)
    wait('Testing hardcopy options: mode="default"')
    g.hardcopy('gp_test.ps', mode='default')

    print '############### test shortcuts ###################################'
    wait('plot Func and Data using shortcuts')
    g.plot('sin(x)', d)

    print '############### test splot #######################################'
    g.splot(gp.Data(d, with='linesp', inline=0))
    wait('Same thing, inline data')
    g.splot(gp.Data(d, with='linesp', inline=1))

    print '############### test GridData and GridFunc #######################'
    # set up x and y values at which the function will be tabulated:
    x = Numeric.arange(35)/2.0
    y = Numeric.arange(30)/10.0 - 1.5
    # Make a 2-d array containing a function of x and y.  First create
    # xm and ym which contain the x and y values in a matrix form that
    # can be `broadcast' into a matrix of the appropriate shape:
    xm = x[:,NewAxis]
    ym = y[NewAxis,:]
    m = (Numeric.sin(xm) + 0.1*xm) - ym**2
    wait('a function of two variables from a GridData file')
    g('set parametric')
    g('set data style lines')
    g('set hidden')
    g('set contour base')
    g.xlabel('x')
    g.ylabel('y')
    g.splot(gp.GridData(m,x,y, binary=0, inline=0))
    wait('Same thing, inline data')
    g.splot(gp.GridData(m,x,y, binary=0, inline=1))

    wait('The same thing using binary mode')
    g.splot(gp.GridData(m,x,y, binary=1))

    wait('The same thing using GridFunc to tabulate function')
    g.splot(gp.GridFunc(lambda x,y: math.sin(x) + 0.1*x - y**2, x,y))

    wait('Use GridFunc in ufunc mode')
    g.splot(gp.GridFunc(lambda x,y: Numeric.sin(x) + 0.1*x - y**2, x,y,
                        ufunc=1, binary=1))

    wait('And now rotate it a bit')
    for view in range(35,70,5):
        g('set view 60, %d' % view)
        g.replot()
        time.sleep(1.0)

    wait(prompt='Press return to end the test.\n')


# when executed, just run main():
if __name__ == '__main__':
    main()