3 # $Id: test.py 302 2008-01-14 22:15:19Z bmcage $
5 # Copyright (C) 1999-2003 Michael Haggerty <mhagger@alum.mit.edu>
7 # This file is licensed under the GNU Lesser General Public License
8 # (LGPL). See LICENSE.txt for details.
10 """test.py -- Exercise the Gnuplot.py module.
12 This module is not meant to be a flashy demonstration; rather it is a
13 thorough test of many combinations of Gnuplot.py features.
17 import os
, time
, math
, tempfile
21 import Gnuplot
, Gnuplot
.PlotItems
, Gnuplot
.funcutils
23 # kludge in case Gnuplot hasn't been installed as a module yet:
27 Gnuplot
.PlotItems
= PlotItems
29 Gnuplot
.funcutils
= funcutils
32 def wait(str=None, prompt
='Press return to show results...\n'):
39 """Exercise the Gnuplot module."""
42 'This program exercises many of the features of Gnuplot.py. The\n'
43 'commands that are actually sent to gnuplot are printed for your\n'
47 wait('Popping up a blank gnuplot window on your screen.')
48 g
= Gnuplot
.Gnuplot(debug
=1)
51 # Make two temporary files:
52 if hasattr(tempfile
, 'mkstemp'):
53 (fd
, filename1
,) = tempfile
.mkstemp(text
=1)
54 f
= os
.fdopen(fd
, 'w')
55 (fd
, filename2
,) = tempfile
.mkstemp(text
=1)
57 filename1
= tempfile
.mktemp()
58 f
= open(filename1
, 'w')
59 filename2
= tempfile
.mktemp()
61 for x
in numpy
.arange(100.)/5. - 10.:
62 f
.write('%s %s %s\n' % (x
, math
.cos(x
), math
.sin(x
)))
65 print '############### test Func ###################################'
66 wait('Plot a gnuplot-generated function')
67 g
.plot(Gnuplot
.Func('sin(x)'))
69 wait('Set title and axis labels and try replot()')
75 wait('Style linespoints')
76 g
.plot(Gnuplot
.Func('sin(x)', with_
='linespoints'))
78 g
.plot(Gnuplot
.Func('sin(x)', title
=None))
79 wait('title="Sine of x"')
80 g
.plot(Gnuplot
.Func('sin(x)', title
='Sine of x'))
82 g
.plot(Gnuplot
.Func('sin(x)', axes
='x2y2', title
='Sine of x'))
84 print 'Change Func attributes after construction:'
85 f
= Gnuplot
.Func('sin(x)')
88 wait('Style linespoints')
89 f
.set_option(with_
='linespoints')
92 f
.set_option(title
=None)
94 wait('title="Sine of x"')
95 f
.set_option(title
='Sine of x')
98 f
.set_option(axes
='x2y2')
101 print '############### test File ###################################'
102 wait('Generate a File from a filename')
103 g
.plot(Gnuplot
.File(filename1
))
106 g
.plot(Gnuplot
.File(filename1
, with_
='lines'))
108 wait('using=1, using=(1,)')
109 g
.plot(Gnuplot
.File(filename1
, using
=1, with_
='lines'),
110 Gnuplot
.File(filename1
, using
=(1,), with_
='points'))
111 wait('using=(1,2), using="1:3"')
112 g
.plot(Gnuplot
.File(filename1
, using
=(1,2)),
113 Gnuplot
.File(filename1
, using
='1:3'))
115 wait('every=5, every=(5,)')
116 g
.plot(Gnuplot
.File(filename1
, every
=5, with_
='lines'),
117 Gnuplot
.File(filename1
, every
=(5,), with_
='points'))
118 wait('every=(10,None,0), every="10::5"')
119 g
.plot(Gnuplot
.File(filename1
, with_
='lines'),
120 Gnuplot
.File(filename1
, every
=(10,None,0)),
121 Gnuplot
.File(filename1
, every
='10::5'))
124 g
.plot(Gnuplot
.File(filename1
, title
=None))
125 wait('title="title"')
126 g
.plot(Gnuplot
.File(filename1
, title
='title'))
128 print 'Change File attributes after construction:'
129 f
= Gnuplot
.File(filename1
)
132 wait('Style linespoints')
133 f
.set_option(with_
='linespoints')
136 f
.set_option(using
=(1,3))
139 f
.set_option(title
=None)
142 print '############### test Data ###################################'
143 x
= numpy
.arange(100)/5. - 10.
146 d
= numpy
.transpose((x
,y1
,y2
))
148 wait('Plot Data against its index')
149 g
.plot(Gnuplot
.Data(y2
, inline
=0))
151 wait('Plot Data, specified column-by-column')
152 g
.plot(Gnuplot
.Data(x
,y2
, inline
=0))
153 wait('Same thing, saved to a file')
154 Gnuplot
.Data(x
,y2
, inline
=0, filename
=filename1
)
155 g
.plot(Gnuplot
.File(filename1
))
156 wait('Same thing, inline data')
157 g
.plot(Gnuplot
.Data(x
,y2
, inline
=1))
159 wait('Plot Data, specified by an array')
160 g
.plot(Gnuplot
.Data(d
, inline
=0))
161 wait('Same thing, saved to a file')
162 Gnuplot
.Data(d
, inline
=0, filename
=filename1
)
163 g
.plot(Gnuplot
.File(filename1
))
164 wait('Same thing, inline data')
165 g
.plot(Gnuplot
.Data(d
, inline
=1))
166 wait('with_="lp 4 4"')
167 g
.plot(Gnuplot
.Data(d
, with_
='lp 4 4'))
169 g
.plot(Gnuplot
.Data(d
, cols
=0))
170 wait('cols=(0,1), cols=(0,2)')
171 g
.plot(Gnuplot
.Data(d
, cols
=(0,1), inline
=0),
172 Gnuplot
.Data(d
, cols
=(0,2), inline
=0))
173 wait('Same thing, saved to files')
174 Gnuplot
.Data(d
, cols
=(0,1), inline
=0, filename
=filename1
)
175 Gnuplot
.Data(d
, cols
=(0,2), inline
=0, filename
=filename2
)
176 g
.plot(Gnuplot
.File(filename1
), Gnuplot
.File(filename2
))
177 wait('Same thing, inline data')
178 g
.plot(Gnuplot
.Data(d
, cols
=(0,1), inline
=1),
179 Gnuplot
.Data(d
, cols
=(0,2), inline
=1))
180 wait('Change title and replot()')
184 g
.plot(Gnuplot
.Data(d
, title
=None))
185 wait('title="Cosine of x"')
186 g
.plot(Gnuplot
.Data(d
, title
='Cosine of x'))
188 print '############### test compute_Data ###########################'
189 x
= numpy
.arange(100)/5. - 10.
191 wait('Plot Data, computed by Gnuplot.py')
193 Gnuplot
.funcutils
.compute_Data(x
, lambda x
: math
.cos(x
), inline
=0)
195 wait('Same thing, saved to a file')
196 Gnuplot
.funcutils
.compute_Data(
197 x
, lambda x
: math
.cos(x
), inline
=0, filename
=filename1
199 g
.plot(Gnuplot
.File(filename1
))
200 wait('Same thing, inline data')
201 g
.plot(Gnuplot
.funcutils
.compute_Data(x
, math
.cos
, inline
=1))
202 wait('with_="lp 4 4"')
203 g
.plot(Gnuplot
.funcutils
.compute_Data(x
, math
.cos
, with_
='lp 4 4'))
205 print '############### test hardcopy ###############################'
206 print '******** Generating postscript file "gp_test.ps" ********'
208 g
.plot(Gnuplot
.Func('cos(0.5*x*x)', with_
='linespoints 2 2',
209 title
='cos(0.5*x^2)'))
210 g
.hardcopy('gp_test.ps')
212 wait('Testing hardcopy options: mode="eps"')
213 g
.hardcopy('gp_test.ps', mode
='eps')
214 wait('Testing hardcopy options: mode="landscape"')
215 g
.hardcopy('gp_test.ps', mode
='landscape')
216 wait('Testing hardcopy options: mode="portrait"')
217 g
.hardcopy('gp_test.ps', mode
='portrait')
218 wait('Testing hardcopy options: eps=1')
219 g
.hardcopy('gp_test.ps', eps
=1)
220 wait('Testing hardcopy options: mode="default"')
221 g
.hardcopy('gp_test.ps', mode
='default')
222 wait('Testing hardcopy options: enhanced=1')
223 g
.hardcopy('gp_test.ps', enhanced
=1)
224 wait('Testing hardcopy options: enhanced=0')
225 g
.hardcopy('gp_test.ps', enhanced
=0)
226 wait('Testing hardcopy options: color=1')
227 g
.hardcopy('gp_test.ps', color
=1)
229 # g.hardcopy('gp_test.ps', color=0, solid=1)
230 # doesn't work here (it doesn't activate the solid option), even
231 # though the command sent to gnuplot looks correct. I'll
232 # tentatively conclude that it is a gnuplot bug. ###
233 wait('Testing hardcopy options: color=0')
234 g
.hardcopy('gp_test.ps', color
=0)
235 wait('Testing hardcopy options: solid=1')
236 g
.hardcopy('gp_test.ps', solid
=1)
237 wait('Testing hardcopy options: duplexing="duplex"')
238 g
.hardcopy('gp_test.ps', solid
=0, duplexing
='duplex')
239 wait('Testing hardcopy options: duplexing="defaultplex"')
240 g
.hardcopy('gp_test.ps', duplexing
='defaultplex')
241 wait('Testing hardcopy options: fontname="Times-Italic"')
242 g
.hardcopy('gp_test.ps', fontname
='Times-Italic')
243 wait('Testing hardcopy options: fontsize=20')
244 g
.hardcopy('gp_test.ps', fontsize
=20)
246 print '******** Generating svg file "gp_test.svg" ********'
248 g
.plot(Gnuplot
.Func('cos(0.5*x*x)', with_
='linespoints 2 2',
249 title
='cos(0.5*x^2)'))
250 g
.hardcopy('gp_test.svg', terminal
='svg')
252 wait('Testing hardcopy svg options: enhanced')
253 g
.hardcopy('gp_test.ps', terminal
='svg', enhanced
='1')
256 print '############### test shortcuts ##############################'
257 wait('plot Func and Data using shortcuts')
260 print '############### test splot ##################################'
262 g
.splot(Gnuplot
.Data(d
, with_
='linesp', inline
=0))
263 wait('Same thing, saved to a file')
264 Gnuplot
.Data(d
, inline
=0, filename
=filename1
)
265 g
.splot(Gnuplot
.File(filename1
, with_
='linesp'))
266 wait('Same thing, inline data')
267 g
.splot(Gnuplot
.Data(d
, with_
='linesp', inline
=1))
269 print '############### test GridData and compute_GridData ##########'
270 # set up x and y values at which the function will be tabulated:
271 x
= numpy
.arange(35)/2.0
272 y
= numpy
.arange(30)/10.0 - 1.5
273 # Make a 2-d array containing a function of x and y. First create
274 # xm and ym which contain the x and y values in a matrix form that
275 # can be `broadcast' into a matrix of the appropriate shape:
276 xm
= x
[:,numpy
.newaxis
]
277 ym
= y
[numpy
.newaxis
,:]
278 m
= (numpy
.sin(xm
) + 0.1*xm
) - ym
**2
279 wait('a function of two variables from a GridData file')
281 g('set data style lines')
283 g('set contour base')
286 g
.splot(Gnuplot
.GridData(m
,x
,y
, binary
=0, inline
=0))
287 wait('Same thing, saved to a file')
288 Gnuplot
.GridData(m
,x
,y
, binary
=0, inline
=0, filename
=filename1
)
289 g
.splot(Gnuplot
.File(filename1
, binary
=0))
290 wait('Same thing, inline data')
291 g
.splot(Gnuplot
.GridData(m
,x
,y
, binary
=0, inline
=1))
293 wait('The same thing using binary mode')
294 g
.splot(Gnuplot
.GridData(m
,x
,y
, binary
=1))
295 wait('Same thing, using binary mode and an intermediate file')
296 Gnuplot
.GridData(m
,x
,y
, binary
=1, filename
=filename1
)
297 g
.splot(Gnuplot
.File(filename1
, binary
=1))
299 wait('The same thing using compute_GridData to tabulate function')
300 g
.splot(Gnuplot
.funcutils
.compute_GridData(
301 x
,y
, lambda x
,y
: math
.sin(x
) + 0.1*x
- y
**2,
303 wait('Same thing, with an intermediate file')
304 Gnuplot
.funcutils
.compute_GridData(
305 x
,y
, lambda x
,y
: math
.sin(x
) + 0.1*x
- y
**2,
307 g
.splot(Gnuplot
.File(filename1
, binary
=1))
309 wait('Use compute_GridData in ufunc and binary mode')
310 g
.splot(Gnuplot
.funcutils
.compute_GridData(
311 x
,y
, lambda x
,y
: numpy
.sin(x
) + 0.1*x
- y
**2,
314 wait('Same thing, with an intermediate file')
315 Gnuplot
.funcutils
.compute_GridData(
316 x
,y
, lambda x
,y
: numpy
.sin(x
) + 0.1*x
- y
**2,
319 g
.splot(Gnuplot
.File(filename1
, binary
=1))
321 wait('And now rotate it a bit')
322 for view
in range(35,70,5):
323 g('set view 60, %d' % view
)
327 wait(prompt
='Press return to end the test.\n')
333 # when executed, just run main():
334 if __name__
== '__main__':