1
2
3
4
5
6
7
8
9
10 """test.py -- Exercise the Gnuplot.py module.
11
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.
14
15 """
16
17 import os, time, math, tempfile
18 try:
19 import numpy
20 except ImportError:
21 import Numeric as numpy
22
23 try:
24 import Gnuplot, Gnuplot.PlotItems, Gnuplot.funcutils
25 except ImportError:
26
27 import __init__
28 Gnuplot = __init__
29 import PlotItems
30 Gnuplot.PlotItems = PlotItems
31 import funcutils
32 Gnuplot.funcutils = funcutils
33
34
35 -def wait(str=None, prompt='Press return to show results...\n'):
36 if str is not None:
37 print str
38 raw_input(prompt)
39
40
42 """Exercise the Gnuplot module."""
43
44 print (
45 'This program exercises many of the features of Gnuplot.py. The\n'
46 'commands that are actually sent to gnuplot are printed for your\n'
47 'enjoyment.'
48 )
49
50 wait('Popping up a blank gnuplot window on your screen.')
51 g = Gnuplot.Gnuplot(debug=1)
52 g.clear()
53
54
55 if hasattr(tempfile, 'mkstemp'):
56 (fd, filename1,) = tempfile.mkstemp(text=1)
57 f = os.fdopen(fd, 'w')
58 (fd, filename2,) = tempfile.mkstemp(text=1)
59 else:
60 filename1 = tempfile.mktemp()
61 f = open(filename1, 'w')
62 filename2 = tempfile.mktemp()
63 try:
64 for x in numpy.arange(100.)/5. - 10.:
65 f.write('%s %s %s\n' % (x, math.cos(x), math.sin(x)))
66 f.close()
67
68 print '############### test Func ###################################'
69 wait('Plot a gnuplot-generated function')
70 g.plot(Gnuplot.Func('sin(x)'))
71
72 wait('Set title and axis labels and try replot()')
73 g.title('Title')
74 g.xlabel('x')
75 g.ylabel('y')
76 g.replot()
77
78 wait('Style linespoints')
79 g.plot(Gnuplot.Func('sin(x)', with_='linespoints'))
80 wait('title=None')
81 g.plot(Gnuplot.Func('sin(x)', title=None))
82 wait('title="Sine of x"')
83 g.plot(Gnuplot.Func('sin(x)', title='Sine of x'))
84 wait('axes=x2y2')
85 g.plot(Gnuplot.Func('sin(x)', axes='x2y2', title='Sine of x'))
86
87 print 'Change Func attributes after construction:'
88 f = Gnuplot.Func('sin(x)')
89 wait('Original')
90 g.plot(f)
91 wait('Style linespoints')
92 f.set_option(with_='linespoints')
93 g.plot(f)
94 wait('title=None')
95 f.set_option(title=None)
96 g.plot(f)
97 wait('title="Sine of x"')
98 f.set_option(title='Sine of x')
99 g.plot(f)
100 wait('axes=x2y2')
101 f.set_option(axes='x2y2')
102 g.plot(f)
103
104 print '############### test File ###################################'
105 wait('Generate a File from a filename')
106 g.plot(Gnuplot.File(filename1))
107
108 wait('Style lines')
109 g.plot(Gnuplot.File(filename1, with_='lines'))
110
111 wait('using=1, using=(1,)')
112 g.plot(Gnuplot.File(filename1, using=1, with_='lines'),
113 Gnuplot.File(filename1, using=(1,), with_='points'))
114 wait('using=(1,2), using="1:3"')
115 g.plot(Gnuplot.File(filename1, using=(1,2)),
116 Gnuplot.File(filename1, using='1:3'))
117
118 wait('every=5, every=(5,)')
119 g.plot(Gnuplot.File(filename1, every=5, with_='lines'),
120 Gnuplot.File(filename1, every=(5,), with_='points'))
121 wait('every=(10,None,0), every="10::5"')
122 g.plot(Gnuplot.File(filename1, with_='lines'),
123 Gnuplot.File(filename1, every=(10,None,0)),
124 Gnuplot.File(filename1, every='10::5'))
125
126 wait('title=None')
127 g.plot(Gnuplot.File(filename1, title=None))
128 wait('title="title"')
129 g.plot(Gnuplot.File(filename1, title='title'))
130
131 print 'Change File attributes after construction:'
132 f = Gnuplot.File(filename1)
133 wait('Original')
134 g.plot(f)
135 wait('Style linespoints')
136 f.set_option(with_='linespoints')
137 g.plot(f)
138 wait('using=(1,3)')
139 f.set_option(using=(1,3))
140 g.plot(f)
141 wait('title=None')
142 f.set_option(title=None)
143 g.plot(f)
144
145 print '############### test Data ###################################'
146 x = numpy.arange(100)/5. - 10.
147 y1 = numpy.cos(x)
148 y2 = numpy.sin(x)
149 d = numpy.transpose((x,y1,y2))
150
151 wait('Plot Data against its index')
152 g.plot(Gnuplot.Data(y2, inline=0))
153
154 wait('Plot Data, specified column-by-column')
155 g.plot(Gnuplot.Data(x,y2, inline=0))
156 wait('Same thing, saved to a file')
157 Gnuplot.Data(x,y2, inline=0, filename=filename1)
158 g.plot(Gnuplot.File(filename1))
159 wait('Same thing, inline data')
160 g.plot(Gnuplot.Data(x,y2, inline=1))
161
162 wait('Plot Data, specified by an array')
163 g.plot(Gnuplot.Data(d, inline=0))
164 wait('Same thing, saved to a file')
165 Gnuplot.Data(d, inline=0, filename=filename1)
166 g.plot(Gnuplot.File(filename1))
167 wait('Same thing, inline data')
168 g.plot(Gnuplot.Data(d, inline=1))
169 wait('with_="lp 4 4"')
170 g.plot(Gnuplot.Data(d, with_='lp 4 4'))
171 wait('cols=0')
172 g.plot(Gnuplot.Data(d, cols=0))
173 wait('cols=(0,1), cols=(0,2)')
174 g.plot(Gnuplot.Data(d, cols=(0,1), inline=0),
175 Gnuplot.Data(d, cols=(0,2), inline=0))
176 wait('Same thing, saved to files')
177 Gnuplot.Data(d, cols=(0,1), inline=0, filename=filename1)
178 Gnuplot.Data(d, cols=(0,2), inline=0, filename=filename2)
179 g.plot(Gnuplot.File(filename1), Gnuplot.File(filename2))
180 wait('Same thing, inline data')
181 g.plot(Gnuplot.Data(d, cols=(0,1), inline=1),
182 Gnuplot.Data(d, cols=(0,2), inline=1))
183 wait('Change title and replot()')
184 g.title('New title')
185 g.replot()
186 wait('title=None')
187 g.plot(Gnuplot.Data(d, title=None))
188 wait('title="Cosine of x"')
189 g.plot(Gnuplot.Data(d, title='Cosine of x'))
190
191 print '############### test compute_Data ###########################'
192 x = numpy.arange(100)/5. - 10.
193
194 wait('Plot Data, computed by Gnuplot.py')
195 g.plot(
196 Gnuplot.funcutils.compute_Data(x, lambda x: math.cos(x), inline=0)
197 )
198 wait('Same thing, saved to a file')
199 Gnuplot.funcutils.compute_Data(
200 x, lambda x: math.cos(x), inline=0, filename=filename1
201 )
202 g.plot(Gnuplot.File(filename1))
203 wait('Same thing, inline data')
204 g.plot(Gnuplot.funcutils.compute_Data(x, math.cos, inline=1))
205 wait('with_="lp 4 4"')
206 g.plot(Gnuplot.funcutils.compute_Data(x, math.cos, with_='lp 4 4'))
207
208 print '############### test hardcopy ###############################'
209 print '******** Generating postscript file "gp_test.ps" ********'
210 wait()
211 g.plot(Gnuplot.Func('cos(0.5*x*x)', with_='linespoints 2 2',
212 title='cos(0.5*x^2)'))
213 g.hardcopy('gp_test.ps')
214
215 wait('Testing hardcopy options: mode="eps"')
216 g.hardcopy('gp_test.ps', mode='eps')
217 wait('Testing hardcopy options: mode="landscape"')
218 g.hardcopy('gp_test.ps', mode='landscape')
219 wait('Testing hardcopy options: mode="portrait"')
220 g.hardcopy('gp_test.ps', mode='portrait')
221 wait('Testing hardcopy options: eps=1')
222 g.hardcopy('gp_test.ps', eps=1)
223 wait('Testing hardcopy options: mode="default"')
224 g.hardcopy('gp_test.ps', mode='default')
225 wait('Testing hardcopy options: enhanced=1')
226 g.hardcopy('gp_test.ps', enhanced=1)
227 wait('Testing hardcopy options: enhanced=0')
228 g.hardcopy('gp_test.ps', enhanced=0)
229 wait('Testing hardcopy options: color=1')
230 g.hardcopy('gp_test.ps', color=1)
231
232
233
234
235
236 wait('Testing hardcopy options: color=0')
237 g.hardcopy('gp_test.ps', color=0)
238 wait('Testing hardcopy options: solid=1')
239 g.hardcopy('gp_test.ps', solid=1)
240 wait('Testing hardcopy options: duplexing="duplex"')
241 g.hardcopy('gp_test.ps', solid=0, duplexing='duplex')
242 wait('Testing hardcopy options: duplexing="defaultplex"')
243 g.hardcopy('gp_test.ps', duplexing='defaultplex')
244 wait('Testing hardcopy options: fontname="Times-Italic"')
245 g.hardcopy('gp_test.ps', fontname='Times-Italic')
246 wait('Testing hardcopy options: fontsize=20')
247 g.hardcopy('gp_test.ps', fontsize=20)
248
249 print '******** Generating svg file "gp_test.svg" ********'
250 wait()
251 g.plot(Gnuplot.Func('cos(0.5*x*x)', with_='linespoints 2 2',
252 title='cos(0.5*x^2)'))
253 g.hardcopy('gp_test.svg', terminal='svg')
254
255 wait('Testing hardcopy svg options: enhanced')
256 g.hardcopy('gp_test.ps', terminal='svg', enhanced='1')
257
258
259 print '############### test shortcuts ##############################'
260 wait('plot Func and Data using shortcuts')
261 g.plot('sin(x)', d)
262
263 print '############### test splot ##################################'
264 wait('a 3-d curve')
265 g.splot(Gnuplot.Data(d, with_='linesp', inline=0))
266 wait('Same thing, saved to a file')
267 Gnuplot.Data(d, inline=0, filename=filename1)
268 g.splot(Gnuplot.File(filename1, with_='linesp'))
269 wait('Same thing, inline data')
270 g.splot(Gnuplot.Data(d, with_='linesp', inline=1))
271
272 print '############### test GridData and compute_GridData ##########'
273
274 x = numpy.arange(35)/2.0
275 y = numpy.arange(30)/10.0 - 1.5
276
277
278
279 xm = x[:,numpy.newaxis]
280 ym = y[numpy.newaxis,:]
281 m = (numpy.sin(xm) + 0.1*xm) - ym**2
282 wait('a function of two variables from a GridData file')
283 g('set parametric')
284 g('set data style lines')
285 g('set hidden')
286 g('set contour base')
287 g.xlabel('x')
288 g.ylabel('y')
289 g.splot(Gnuplot.GridData(m,x,y, binary=0, inline=0))
290 wait('Same thing, saved to a file')
291 Gnuplot.GridData(m,x,y, binary=0, inline=0, filename=filename1)
292 g.splot(Gnuplot.File(filename1, binary=0))
293 wait('Same thing, inline data')
294 g.splot(Gnuplot.GridData(m,x,y, binary=0, inline=1))
295
296 wait('The same thing using binary mode')
297 g.splot(Gnuplot.GridData(m,x,y, binary=1))
298 wait('Same thing, using binary mode and an intermediate file')
299 Gnuplot.GridData(m,x,y, binary=1, filename=filename1)
300 g.splot(Gnuplot.File(filename1, binary=1))
301
302 wait('The same thing using compute_GridData to tabulate function')
303 g.splot(Gnuplot.funcutils.compute_GridData(
304 x,y, lambda x,y: math.sin(x) + 0.1*x - y**2,
305 ))
306 wait('Same thing, with an intermediate file')
307 Gnuplot.funcutils.compute_GridData(
308 x,y, lambda x,y: math.sin(x) + 0.1*x - y**2,
309 filename=filename1)
310 g.splot(Gnuplot.File(filename1, binary=1))
311
312 wait('Use compute_GridData in ufunc and binary mode')
313 g.splot(Gnuplot.funcutils.compute_GridData(
314 x,y, lambda x,y: numpy.sin(x) + 0.1*x - y**2,
315 ufunc=1, binary=1,
316 ))
317 wait('Same thing, with an intermediate file')
318 Gnuplot.funcutils.compute_GridData(
319 x,y, lambda x,y: numpy.sin(x) + 0.1*x - y**2,
320 ufunc=1, binary=1,
321 filename=filename1)
322 g.splot(Gnuplot.File(filename1, binary=1))
323
324 wait('And now rotate it a bit')
325 for view in range(35,70,5):
326 g('set view 60, %d' % view)
327 g.replot()
328 time.sleep(1.0)
329
330 wait(prompt='Press return to end the test.\n')
331 finally:
332 os.unlink(filename1)
333 os.unlink(filename2)
334
335
336
337 if __name__ == '__main__':
338 main()
339