50
42
"""Exercise the Gnuplot module."""
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'
52
50
wait('Popping up a blank gnuplot window on your screen.')
51
g = Gnuplot.Gnuplot(debug=1)
56
54
# Make a temporary file:
57
55
filename1 = tempfile.mktemp()
58
56
f = open(filename1, 'w')
59
for x in Numeric.arange(100)/5. - 10.:
60
f.write('%s %s %s\n' % (x, math.cos(x), math.sin(x)))
62
# ensure that file will be deleted upon exit:
63
file1 = Gnuplot.PlotItems.TempFile(filename1)
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))
104
wait('Generate a File given a TempFile object')
105
g.plot(Gnuplot.File(file1))
108
g.plot(Gnuplot.File(filename1, with='lines'))
109
wait('using=1, using=(1,)')
110
g.plot(Gnuplot.File(filename1, using=1, with='lines'),
111
Gnuplot.File(filename1, using=(1,), with='points'))
112
wait('using=(1,2), using="1:3"')
113
g.plot(Gnuplot.File(filename1, using=(1,2)),
114
Gnuplot.File(filename1, using='1:3'))
116
g.plot(Gnuplot.File(filename1, title=None))
117
wait('title="title"')
118
g.plot(Gnuplot.File(filename1, title='title'))
120
print 'Change File attributes after construction:'
121
f = Gnuplot.File(filename1)
124
wait('Style linespoints')
125
f.set_option(with='linespoints')
128
f.set_option(using=(1,3))
131
f.set_option(title=None)
134
print '############### test Data ########################################'
135
x = Numeric.arange(100)/5. - 10.
138
d = Numeric.transpose((x,y1,y2))
140
wait('Plot Data, specified column-by-column')
141
g.plot(Gnuplot.Data(x,y2, inline=0))
142
wait('Same thing, inline data')
143
g.plot(Gnuplot.Data(x,y2, inline=1))
145
wait('Plot Data, specified by an array')
146
g.plot(Gnuplot.Data(d, inline=0))
147
wait('Same thing, inline data')
148
g.plot(Gnuplot.Data(d, inline=1))
149
wait('with="lp 4 4"')
150
g.plot(Gnuplot.Data(d, with='lp 4 4'))
152
g.plot(Gnuplot.Data(d, cols=0))
153
wait('cols=(0,1), cols=(0,2)')
154
g.plot(Gnuplot.Data(d, cols=(0,1), inline=0),
155
Gnuplot.Data(d, cols=(0,2), inline=0))
156
wait('Same thing, inline data')
157
g.plot(Gnuplot.Data(d, cols=(0,1), inline=1),
158
Gnuplot.Data(d, cols=(0,2), inline=1))
159
wait('Change title and replot()')
163
g.plot(Gnuplot.Data(d, title=None))
164
wait('title="Cosine of x"')
165
g.plot(Gnuplot.Data(d, title='Cosine of x'))
167
print '############### test compute_Data ################################'
168
x = Numeric.arange(100)/5. - 10.
170
wait('Plot Data, computed by Gnuplot.py')
171
g.plot(Gnuplot.funcutils.compute_Data(x, lambda x: math.cos(x), inline=0))
172
wait('Same thing, inline data')
173
g.plot(Gnuplot.funcutils.compute_Data(x, math.cos, inline=1))
174
wait('with="lp 4 4"')
175
g.plot(Gnuplot.funcutils.compute_Data(x, math.cos, with='lp 4 4'))
177
print '############### test hardcopy ####################################'
178
print '******** Generating postscript file "gp_test.ps" ********'
180
g.plot(Gnuplot.Func('cos(0.5*x*x)', with='linespoints 2 2',
181
title='cos(0.5*x^2)'))
182
g.hardcopy('gp_test.ps')
184
wait('Testing hardcopy options: mode="landscape"')
185
g.hardcopy('gp_test.ps', mode='landscape')
186
wait('Testing hardcopy options: mode="portrait"')
187
g.hardcopy('gp_test.ps', mode='portrait')
188
wait('Testing hardcopy options: mode="eps"')
189
g.hardcopy('gp_test.ps', mode='eps')
190
wait('Testing hardcopy options: eps=1')
191
g.hardcopy('gp_test.ps', eps=1)
192
wait('Testing hardcopy options: enhanced=1')
193
g.hardcopy('gp_test.ps', eps=0, enhanced=1)
194
wait('Testing hardcopy options: enhanced=0')
195
g.hardcopy('gp_test.ps', enhanced=0)
196
wait('Testing hardcopy options: color=1')
197
g.hardcopy('gp_test.ps', color=1)
198
wait('Testing hardcopy options: solid=1')
199
g.hardcopy('gp_test.ps', color=0, solid=1)
200
wait('Testing hardcopy options: duplexing="duplex"')
201
g.hardcopy('gp_test.ps', solid=0, duplexing='duplex')
202
wait('Testing hardcopy options: duplexing="defaultplex"')
203
g.hardcopy('gp_test.ps', duplexing='defaultplex')
204
wait('Testing hardcopy options: fontname="Times-Italic"')
205
g.hardcopy('gp_test.ps', fontname='Times-Italic')
206
wait('Testing hardcopy options: fontsize=20')
207
g.hardcopy('gp_test.ps', fontsize=20)
208
wait('Testing hardcopy options: mode="default"')
209
g.hardcopy('gp_test.ps', mode='default')
211
print '############### test shortcuts ###################################'
212
wait('plot Func and Data using shortcuts')
215
print '############### test splot #######################################'
217
g.splot(Gnuplot.Data(d, with='linesp', inline=0))
218
wait('Same thing, inline data')
219
g.splot(Gnuplot.Data(d, with='linesp', inline=1))
221
print '############### test GridData and compute_GridData ###############'
222
# set up x and y values at which the function will be tabulated:
223
x = Numeric.arange(35)/2.0
224
y = Numeric.arange(30)/10.0 - 1.5
225
# Make a 2-d array containing a function of x and y. First create
226
# xm and ym which contain the x and y values in a matrix form that
227
# can be `broadcast' into a matrix of the appropriate shape:
230
m = (Numeric.sin(xm) + 0.1*xm) - ym**2
231
wait('a function of two variables from a GridData file')
233
g('set data style lines')
235
g('set contour base')
238
g.splot(Gnuplot.GridData(m,x,y, binary=0, inline=0))
239
wait('Same thing, inline data')
240
g.splot(Gnuplot.GridData(m,x,y, binary=0, inline=1))
242
wait('The same thing using binary mode')
243
g.splot(Gnuplot.GridData(m,x,y, binary=1))
245
wait('The same thing using compute_GridData to tabulate function')
246
g.splot(Gnuplot.funcutils.compute_GridData(
247
x,y, lambda x,y: math.sin(x) + 0.1*x - y**2,
250
wait('Use compute_GridData in ufunc and binary mode')
251
g.splot(Gnuplot.funcutils.compute_GridData(
252
x,y, lambda x,y: Numeric.sin(x) + 0.1*x - y**2,
256
wait('And now rotate it a bit')
257
for view in range(35,70,5):
258
g('set view 60, %d' % view)
262
wait(prompt='Press return to end the test.\n')
58
for x in Numeric.arange(100)/5. - 10.:
59
f.write('%s %s %s\n' % (x, math.cos(x), math.sin(x)))
62
print '############### test Func ###################################'
63
wait('Plot a gnuplot-generated function')
64
g.plot(Gnuplot.Func('sin(x)'))
66
wait('Set title and axis labels and try replot()')
72
wait('Style linespoints')
73
g.plot(Gnuplot.Func('sin(x)', with='linespoints'))
75
g.plot(Gnuplot.Func('sin(x)', title=None))
76
wait('title="Sine of x"')
77
g.plot(Gnuplot.Func('sin(x)', title='Sine of x'))
79
g.plot(Gnuplot.Func('sin(x)', axes='x2y2', title='Sine of x'))
81
print 'Change Func attributes after construction:'
82
f = Gnuplot.Func('sin(x)')
85
wait('Style linespoints')
86
f.set_option(with='linespoints')
89
f.set_option(title=None)
91
wait('title="Sine of x"')
92
f.set_option(title='Sine of x')
95
f.set_option(axes='x2y2')
98
print '############### test File ###################################'
99
wait('Generate a File from a filename')
100
g.plot(Gnuplot.File(filename1))
103
g.plot(Gnuplot.File(filename1, with='lines'))
105
wait('using=1, using=(1,)')
106
g.plot(Gnuplot.File(filename1, using=1, with='lines'),
107
Gnuplot.File(filename1, using=(1,), with='points'))
108
wait('using=(1,2), using="1:3"')
109
g.plot(Gnuplot.File(filename1, using=(1,2)),
110
Gnuplot.File(filename1, using='1:3'))
112
wait('every=5, every=(5,)')
113
g.plot(Gnuplot.File(filename1, every=5, with='lines'),
114
Gnuplot.File(filename1, every=(5,), with='points'))
115
wait('every=(10,None,0), every="10::5"')
116
g.plot(Gnuplot.File(filename1, with='lines'),
117
Gnuplot.File(filename1, every=(10,None,0)),
118
Gnuplot.File(filename1, every='10::5'))
121
g.plot(Gnuplot.File(filename1, title=None))
122
wait('title="title"')
123
g.plot(Gnuplot.File(filename1, title='title'))
125
print 'Change File attributes after construction:'
126
f = Gnuplot.File(filename1)
129
wait('Style linespoints')
130
f.set_option(with='linespoints')
133
f.set_option(using=(1,3))
136
f.set_option(title=None)
139
print '############### test Data ###################################'
140
x = Numeric.arange(100)/5. - 10.
143
d = Numeric.transpose((x,y1,y2))
145
wait('Plot Data against its index')
146
g.plot(Gnuplot.Data(y2, inline=0))
148
wait('Plot Data, specified column-by-column')
149
g.plot(Gnuplot.Data(x,y2, inline=0))
150
wait('Same thing, inline data')
151
g.plot(Gnuplot.Data(x,y2, inline=1))
153
wait('Plot Data, specified by an array')
154
g.plot(Gnuplot.Data(d, inline=0))
155
wait('Same thing, inline data')
156
g.plot(Gnuplot.Data(d, inline=1))
157
wait('with="lp 4 4"')
158
g.plot(Gnuplot.Data(d, with='lp 4 4'))
160
g.plot(Gnuplot.Data(d, cols=0))
161
wait('cols=(0,1), cols=(0,2)')
162
g.plot(Gnuplot.Data(d, cols=(0,1), inline=0),
163
Gnuplot.Data(d, cols=(0,2), inline=0))
164
wait('Same thing, inline data')
165
g.plot(Gnuplot.Data(d, cols=(0,1), inline=1),
166
Gnuplot.Data(d, cols=(0,2), inline=1))
167
wait('Change title and replot()')
171
g.plot(Gnuplot.Data(d, title=None))
172
wait('title="Cosine of x"')
173
g.plot(Gnuplot.Data(d, title='Cosine of x'))
175
print '############### test compute_Data ###########################'
176
x = Numeric.arange(100)/5. - 10.
178
wait('Plot Data, computed by Gnuplot.py')
179
g.plot(Gnuplot.funcutils.compute_Data(x, lambda x: math.cos(x), inline=0))
180
wait('Same thing, inline data')
181
g.plot(Gnuplot.funcutils.compute_Data(x, math.cos, inline=1))
182
wait('with="lp 4 4"')
183
g.plot(Gnuplot.funcutils.compute_Data(x, math.cos, with='lp 4 4'))
185
print '############### test hardcopy ###############################'
186
print '******** Generating postscript file "gp_test.ps" ********'
188
g.plot(Gnuplot.Func('cos(0.5*x*x)', with='linespoints 2 2',
189
title='cos(0.5*x^2)'))
190
g.hardcopy('gp_test.ps')
192
wait('Testing hardcopy options: mode="eps"')
193
g.hardcopy('gp_test.ps', mode='eps')
194
wait('Testing hardcopy options: mode="landscape"')
195
g.hardcopy('gp_test.ps', mode='landscape')
196
wait('Testing hardcopy options: mode="portrait"')
197
g.hardcopy('gp_test.ps', mode='portrait')
198
wait('Testing hardcopy options: eps=1')
199
g.hardcopy('gp_test.ps', eps=1)
200
wait('Testing hardcopy options: mode="default"')
201
g.hardcopy('gp_test.ps', mode='default')
202
wait('Testing hardcopy options: enhanced=1')
203
g.hardcopy('gp_test.ps', enhanced=1)
204
wait('Testing hardcopy options: enhanced=0')
205
g.hardcopy('gp_test.ps', enhanced=0)
206
wait('Testing hardcopy options: color=1')
207
g.hardcopy('gp_test.ps', color=1)
209
# g.hardcopy('gp_test.ps', color=0, solid=1)
210
# doesn't work here (it doesn't activate the solid option), even
211
# though the command sent to gnuplot looks correct. I'll
212
# tentatively conclude that it is a gnuplot bug. ###
213
wait('Testing hardcopy options: color=0')
214
g.hardcopy('gp_test.ps', color=0)
215
wait('Testing hardcopy options: solid=1')
216
g.hardcopy('gp_test.ps', solid=1)
217
wait('Testing hardcopy options: duplexing="duplex"')
218
g.hardcopy('gp_test.ps', solid=0, duplexing='duplex')
219
wait('Testing hardcopy options: duplexing="defaultplex"')
220
g.hardcopy('gp_test.ps', duplexing='defaultplex')
221
wait('Testing hardcopy options: fontname="Times-Italic"')
222
g.hardcopy('gp_test.ps', fontname='Times-Italic')
223
wait('Testing hardcopy options: fontsize=20')
224
g.hardcopy('gp_test.ps', fontsize=20)
226
print '############### test shortcuts ##############################'
227
wait('plot Func and Data using shortcuts')
230
print '############### test splot ##################################'
232
g.splot(Gnuplot.Data(d, with='linesp', inline=0))
233
wait('Same thing, inline data')
234
g.splot(Gnuplot.Data(d, with='linesp', inline=1))
236
print '############### test GridData and compute_GridData ##########'
237
# set up x and y values at which the function will be tabulated:
238
x = Numeric.arange(35)/2.0
239
y = Numeric.arange(30)/10.0 - 1.5
240
# Make a 2-d array containing a function of x and y. First create
241
# xm and ym which contain the x and y values in a matrix form that
242
# can be `broadcast' into a matrix of the appropriate shape:
245
m = (Numeric.sin(xm) + 0.1*xm) - ym**2
246
wait('a function of two variables from a GridData file')
248
g('set data style lines')
250
g('set contour base')
253
g.splot(Gnuplot.GridData(m,x,y, binary=0, inline=0))
254
wait('Same thing, inline data')
255
g.splot(Gnuplot.GridData(m,x,y, binary=0, inline=1))
257
wait('The same thing using binary mode')
258
g.splot(Gnuplot.GridData(m,x,y, binary=1))
260
wait('The same thing using compute_GridData to tabulate function')
261
g.splot(Gnuplot.funcutils.compute_GridData(
262
x,y, lambda x,y: math.sin(x) + 0.1*x - y**2,
265
wait('Use compute_GridData in ufunc and binary mode')
266
g.splot(Gnuplot.funcutils.compute_GridData(
267
x,y, lambda x,y: Numeric.sin(x) + 0.1*x - y**2,
271
wait('And now rotate it a bit')
272
for view in range(35,70,5):
273
g('set view 60, %d' % view)
277
wait(prompt='Press return to end the test.\n')
265
282
# when executed, just run main():