/[escript]/trunk/pyvisi_old/examples/offsetLinePlotExample.py
ViewVC logotype

Contents of /trunk/pyvisi_old/examples/offsetLinePlotExample.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 786 - (show annotations)
Tue Jul 25 04:58:05 2006 UTC (13 years, 1 month ago) by gross
File MIME type: text/x-python
File size: 9705 byte(s)
switched off pyvisi
1 # $Id: offsetLinePlotExample.py,v 1.3 2005/04/29 00:35:07 paultcochrane Exp $
2 """
3 Example of plotting multiple curves offset from each other with pyvisi
4
5 This is especially handy for people plotting seismic data
6
7 @var __author__: name of author
8 @var __license__: licence agreement
9 @var __copyright__: copyrights
10 @var __url__: url entry point on documentation
11 @var __version__: version
12 @var __date__: date of the version
13 """
14
15 __copyright__=""" Copyright (c) 2006 by ACcESS MNRF
16 http://www.access.edu.au
17 Primary Business: Queensland, Australia"""
18 __license__="""Licensed under the Open Software License version 3.0
19 http://www.opensource.org/licenses/osl-3.0.php"""
20 __author__="Paul Cochrane"
21 __url__="http://www.iservo.edu.au/esys"
22 __version__="$Revision$"
23 __date__="$Date$"
24
25
26 # what plotting method are we using?
27 method = 'pyvisi'
28
29 # set up some data to plot
30 from Numeric import *
31
32 x = arange(0,2*pi,0.01, typecode=Float)
33 y1 = sin(x)
34 y2 = cos(x)
35 y3 = cos(x)**2
36 y4 = sin(2*x)
37 y5 = cos(3*x)
38 y6 = sin(20*x)
39
40 # plot it using one of the three methods
41 if method == 'pyvisi':
42
43 # example code for how a user would write a script in pyvisi
44 from esys.pyvisi import * # base level visualisation stuff
45 #from esys.pyvisi.utils import * # pyvisi specific utils
46 # import the objects to render the scene using the specific renderer
47 #from esys.pyvisi.renderers.gnuplot import * # gnuplot
48 #from esys.pyvisi.renderers.vtk import * # vtk
49 from esys.pyvisi.renderers.plplot import * # plplot
50
51 # define the scene object
52 # a Scene is a container for all of the kinds of things you want to put
53 # into your plot for instance, images, meshes, arrow/vector/quiver plots,
54 # contour plots, spheres etc.
55 scene = Scene()
56
57 # create a LinePlot object
58 plot = LinePlot(scene)
59
60 # add some helpful info to the plot
61 plot.title = 'Example 2D plot with offsets'
62 plot.xlabel = 'x'
63 plot.ylabel = 'y'
64
65 plot.linestyle = 'lines'
66
67 # assign some data to the plot
68 plot.setData(x, y1, y2, y3, y4, y5, y6, offset=True)
69
70 # render the scene to screen
71 scene.render(pause=True, interactive=True)
72
73 # save the scene to file
74 # save as png
75 plot.setData(x, y1, y2, y3, y4, y5, y6, offset=True)
76 # have to do this now because we've already
77 # render()ed the scene. This requirement
78 # will be removed in the future.
79 scene.save(fname="offsetLinePlotExample.png", format=PngImage())
80
81 # save as postscript
82 plot.setData(x, y1, y2, y3, y4, y5, y6, offset=True)
83 # have to do this now because we've already
84 # save()d the scene. This requirement will
85 # be removed in the future.
86 scene.save(fname="offsetLinePlotExample.ps", format=PsImage())
87
88 elif method == 'gnuplot':
89 #### original gnuplot code
90
91 import Gnuplot
92
93 # set the plot up
94 _gnuplot = Gnuplot.Gnuplot()
95 _gnuplot.title('Example 2D plot with offsets')
96 _gnuplot.xlabel('x')
97 _gnuplot.ylabel('y')
98
99 ### set up the data
100 # concatenate the data
101 yAll = concatenate( [y1, y2, y3, y4, y5, y6] )
102
103 yMax = max(yAll)
104 yMin = min(yAll)
105
106 # keep the data apart a bit
107 const = 0.1*(yMax-yMin)
108
109 # don't need to worry about y1: it's the first data series
110 shift = yMax - yMin + const
111 y2 = y2 + shift
112 y3 = y3 + 2*shift
113 y4 = y4 + 3*shift
114 y5 = y5 + 4*shift
115 y6 = y6 + 5*shift
116
117 _data1 = Gnuplot.Data(x, y1, with='lines')
118 _data2 = Gnuplot.Data(x, y2, with='lines')
119 _data3 = Gnuplot.Data(x, y3, with='lines')
120 _data4 = Gnuplot.Data(x, y4, with='lines')
121 _data5 = Gnuplot.Data(x, y5, with='lines')
122 _data6 = Gnuplot.Data(x, y6, with='lines')
123
124 # plot it
125 _gnuplot.plot(_data1, _data2, _data3, _data4, _data5, _data6)
126
127 # save it to file
128 _gnuplot('set terminal png')
129 _gnuplot('set output "offsetLinePlotExample.png"')
130 _gnuplot.plot(_data1, _data2, _data3)
131
132 raw_input('Press enter to continue...\n')
133
134 elif method == 'vtk':
135 #### original vtk code
136
137 import vtk
138
139 # set up the renderer and the render window
140 _ren = vtk.vtkRenderer()
141 _renWin = vtk.vtkRenderWindow()
142 _renWin.AddRenderer(_ren)
143
144 # do a quick check to make sure x and y are same length
145 if len(x) != len(y1):
146 raise ValueError, "x and y vectors must be same length"
147
148 if len(x) != len(y2):
149 raise ValueError, "x and y vectors must be same length"
150
151 if len(x) != len(y3):
152 raise ValueError, "x and y vectors must be same length"
153
154 if len(x) != len(y4):
155 raise ValueError, "x and y vectors must be same length"
156
157 if len(x) != len(y5):
158 raise ValueError, "x and y vectors must be same length"
159
160 if len(x) != len(y6):
161 raise ValueError, "x and y vectors must be same length"
162
163 ### set up the data to be offset
164 # concatenate the data
165 yAll = concatenate( [y1, y2, y3, y4, y5, y6] )
166
167 yMax = max(yAll)
168 yMin = min(yAll)
169
170 # keep the data apart a bit
171 const = 0.1*(yMax-yMin)
172
173 # don't need to worry about y1: it's the first data series
174 shift = yMax - yMin + const
175 y2 = y2 + shift
176 y3 = y3 + 2*shift
177 y4 = y4 + 3*shift
178 y5 = y5 + 4*shift
179 y6 = y6 + 5*shift
180
181 # set up the x and y data arrays to be able to accept the data (code
182 # here adapted from the C++ of a vtk-users mailing list reply by Sander
183 # Niemeijer)
184 _xData = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
185 _xData.SetNumberOfTuples(len(x))
186
187 _yData1 = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
188 _yData1.SetNumberOfTuples(len(y1))
189
190 _yData2 = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
191 _yData2.SetNumberOfTuples(len(y2))
192
193 _yData3 = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
194 _yData3.SetNumberOfTuples(len(y3))
195
196 _yData4 = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
197 _yData4.SetNumberOfTuples(len(y4))
198
199 _yData5 = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
200 _yData5.SetNumberOfTuples(len(y5))
201
202 _yData6 = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
203 _yData6.SetNumberOfTuples(len(y6))
204
205 # put the data into the data arrays
206 for i in range(len(x)):
207 _xData.SetTuple1(i,x[i])
208 _yData1.SetTuple1(i,y1[i])
209 _yData2.SetTuple1(i,y2[i])
210 _yData3.SetTuple1(i,y3[i])
211 _yData4.SetTuple1(i,y4[i])
212 _yData5.SetTuple1(i,y5[i])
213 _yData6.SetTuple1(i,y6[i])
214
215 # create a field data object
216 # (I think this is as a containter to hold the data arrays)
217 _fieldData1 = vtk.vtkFieldData()
218 _fieldData1.AllocateArrays(2)
219 _fieldData1.AddArray(_xData)
220 _fieldData1.AddArray(_yData1)
221
222 _fieldData2 = vtk.vtkFieldData()
223 _fieldData2.AllocateArrays(2)
224 _fieldData2.AddArray(_xData)
225 _fieldData2.AddArray(_yData2)
226
227 _fieldData3 = vtk.vtkFieldData()
228 _fieldData3.AllocateArrays(2)
229 _fieldData3.AddArray(_xData)
230 _fieldData3.AddArray(_yData3)
231
232 _fieldData4 = vtk.vtkFieldData()
233 _fieldData4.AllocateArrays(2)
234 _fieldData4.AddArray(_xData)
235 _fieldData4.AddArray(_yData4)
236
237 _fieldData5 = vtk.vtkFieldData()
238 _fieldData5.AllocateArrays(2)
239 _fieldData5.AddArray(_xData)
240 _fieldData5.AddArray(_yData5)
241
242 _fieldData6 = vtk.vtkFieldData()
243 _fieldData6.AllocateArrays(2)
244 _fieldData6.AddArray(_xData)
245 _fieldData6.AddArray(_yData6)
246
247 # now put the field data object into a data object so that can add it as
248 # input to the xyPlotActor
249 _dataObject1 = vtk.vtkDataObject()
250 _dataObject1.SetFieldData(_fieldData1)
251
252 _dataObject2 = vtk.vtkDataObject()
253 _dataObject2.SetFieldData(_fieldData2)
254
255 _dataObject3 = vtk.vtkDataObject()
256 _dataObject3.SetFieldData(_fieldData3)
257
258 _dataObject4 = vtk.vtkDataObject()
259 _dataObject4.SetFieldData(_fieldData4)
260
261 _dataObject5 = vtk.vtkDataObject()
262 _dataObject5.SetFieldData(_fieldData5)
263
264 _dataObject6 = vtk.vtkDataObject()
265 _dataObject6.SetFieldData(_fieldData6)
266
267 # set up the actor
268 _plot = vtk.vtkXYPlotActor()
269 _plot.AddDataObjectInput(_dataObject1)
270 _plot.AddDataObjectInput(_dataObject2)
271 _plot.AddDataObjectInput(_dataObject3)
272 _plot.AddDataObjectInput(_dataObject4)
273 _plot.AddDataObjectInput(_dataObject5)
274 _plot.AddDataObjectInput(_dataObject6)
275
276 # set the title and stuff
277 _plot.SetTitle("Example 2D plot with offsets")
278 _plot.SetXTitle("x")
279 _plot.SetYTitle("y")
280 _plot.SetXValuesToValue()
281
282 # set which parts of the data object are to be used for which axis
283 _plot.SetDataObjectXComponent(0,0)
284 _plot.SetDataObjectYComponent(0,1)
285 _plot.SetDataObjectYComponent(1,1)
286 _plot.SetDataObjectYComponent(2,1)
287 _plot.SetDataObjectYComponent(3,1)
288 _plot.SetDataObjectYComponent(4,1)
289 _plot.SetDataObjectYComponent(5,1)
290
291 # add the actor
292 _ren.AddActor2D(_plot)
293
294 # render the scene
295 _iren = vtk.vtkRenderWindowInteractor()
296 _iren.SetRenderWindow(_renWin)
297 _iren.Initialize()
298 _renWin.Render()
299 _iren.Start()
300
301 # convert the render window to an image
302 _win2imgFilter = vtk.vtkWindowToImageFilter()
303 _win2imgFilter.SetInput(_renWin)
304
305 # save the image to file
306 _outWriter = vtk.vtkPNGWriter()
307 _outWriter.SetInput(_win2imgFilter.GetOutput())
308 _outWriter.SetFileName("offsetLinePlotExample.png")
309 _outWriter.Write()
310
311 # pause for input
312 #raw_input('Press enter to continue...\n')
313
314 else:
315 print "Eeek! What plotting method am I supposed to use???"
316
317 # vim: expandtab shiftwidth=4:
318

  ViewVC Help
Powered by ViewVC 1.1.26