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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26