/[escript]/trunk/pyvisi/test/python/vtk/offsetLinePlot.py
ViewVC logotype

Contents of /trunk/pyvisi/test/python/vtk/offsetLinePlot.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 604 - (show annotations)
Mon Mar 20 02:23:14 2006 UTC (13 years, 7 months ago) by gross
File MIME type: text/x-python
File size: 5223 byte(s)
example cleanup
1 # $Id: offsetLinePlot.py,v 1.1 2005/11/08 06:51:39 paultcochrane Exp $
2 """
3 Example of plotting multiple curves offset from each other
4
5 This is especially handy for people plotting seismic data
6 """
7
8 # set up some data to plot
9 from Numeric import *
10
11 x = arange(0,2*pi,0.01, typecode=Float)
12 y1 = sin(x)
13 y2 = cos(x)
14 y3 = cos(x)**2
15 y4 = sin(2*x)
16 y5 = cos(3*x)
17 y6 = sin(20*x)
18
19 #### original vtk code
20
21 import vtk
22
23 # set up the renderer and the render window
24 _ren = vtk.vtkRenderer()
25 _renWin = vtk.vtkRenderWindow()
26 _renWin.AddRenderer(_ren)
27
28 # do a quick check to make sure x and y are same length
29 if len(x) != len(y1):
30 raise ValueError, "x and y vectors must be same length"
31
32 if len(x) != len(y2):
33 raise ValueError, "x and y vectors must be same length"
34
35 if len(x) != len(y3):
36 raise ValueError, "x and y vectors must be same length"
37
38 if len(x) != len(y4):
39 raise ValueError, "x and y vectors must be same length"
40
41 if len(x) != len(y5):
42 raise ValueError, "x and y vectors must be same length"
43
44 if len(x) != len(y6):
45 raise ValueError, "x and y vectors must be same length"
46
47 ### set up the data to be offset
48 # concatenate the data
49 yAll = concatenate( [y1, y2, y3, y4, y5, y6] )
50
51 yMax = max(yAll)
52 yMin = min(yAll)
53
54 # keep the data apart a bit
55 const = 0.1*(yMax-yMin)
56
57 # don't need to worry about y1: it's the first data series
58 shift = yMax - yMin + const
59 y2 = y2 + shift
60 y3 = y3 + 2*shift
61 y4 = y4 + 3*shift
62 y5 = y5 + 4*shift
63 y6 = y6 + 5*shift
64
65 # set up the x and y data arrays to be able to accept the data (code
66 # here adapted from the C++ of a vtk-users mailing list reply by Sander
67 # Niemeijer)
68 _xData = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
69 _xData.SetNumberOfTuples(len(x))
70
71 _yData1 = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
72 _yData1.SetNumberOfTuples(len(y1))
73
74 _yData2 = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
75 _yData2.SetNumberOfTuples(len(y2))
76
77 _yData3 = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
78 _yData3.SetNumberOfTuples(len(y3))
79
80 _yData4 = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
81 _yData4.SetNumberOfTuples(len(y4))
82
83 _yData5 = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
84 _yData5.SetNumberOfTuples(len(y5))
85
86 _yData6 = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
87 _yData6.SetNumberOfTuples(len(y6))
88
89 # put the data into the data arrays
90 for i in range(len(x)):
91 _xData.SetTuple1(i,x[i])
92 _yData1.SetTuple1(i,y1[i])
93 _yData2.SetTuple1(i,y2[i])
94 _yData3.SetTuple1(i,y3[i])
95 _yData4.SetTuple1(i,y4[i])
96 _yData5.SetTuple1(i,y5[i])
97 _yData6.SetTuple1(i,y6[i])
98
99 # create a field data object
100 # (I think this is as a containter to hold the data arrays)
101 _fieldData1 = vtk.vtkFieldData()
102 _fieldData1.AllocateArrays(2)
103 _fieldData1.AddArray(_xData)
104 _fieldData1.AddArray(_yData1)
105
106 _fieldData2 = vtk.vtkFieldData()
107 _fieldData2.AllocateArrays(2)
108 _fieldData2.AddArray(_xData)
109 _fieldData2.AddArray(_yData2)
110
111 _fieldData3 = vtk.vtkFieldData()
112 _fieldData3.AllocateArrays(2)
113 _fieldData3.AddArray(_xData)
114 _fieldData3.AddArray(_yData3)
115
116 _fieldData4 = vtk.vtkFieldData()
117 _fieldData4.AllocateArrays(2)
118 _fieldData4.AddArray(_xData)
119 _fieldData4.AddArray(_yData4)
120
121 _fieldData5 = vtk.vtkFieldData()
122 _fieldData5.AllocateArrays(2)
123 _fieldData5.AddArray(_xData)
124 _fieldData5.AddArray(_yData5)
125
126 _fieldData6 = vtk.vtkFieldData()
127 _fieldData6.AllocateArrays(2)
128 _fieldData6.AddArray(_xData)
129 _fieldData6.AddArray(_yData6)
130
131 # now put the field data object into a data object so that can add it as
132 # input to the xyPlotActor
133 _dataObject1 = vtk.vtkDataObject()
134 _dataObject1.SetFieldData(_fieldData1)
135
136 _dataObject2 = vtk.vtkDataObject()
137 _dataObject2.SetFieldData(_fieldData2)
138
139 _dataObject3 = vtk.vtkDataObject()
140 _dataObject3.SetFieldData(_fieldData3)
141
142 _dataObject4 = vtk.vtkDataObject()
143 _dataObject4.SetFieldData(_fieldData4)
144
145 _dataObject5 = vtk.vtkDataObject()
146 _dataObject5.SetFieldData(_fieldData5)
147
148 _dataObject6 = vtk.vtkDataObject()
149 _dataObject6.SetFieldData(_fieldData6)
150
151 # set up the actor
152 _plot = vtk.vtkXYPlotActor()
153 _plot.AddDataObjectInput(_dataObject1)
154 _plot.AddDataObjectInput(_dataObject2)
155 _plot.AddDataObjectInput(_dataObject3)
156 _plot.AddDataObjectInput(_dataObject4)
157 _plot.AddDataObjectInput(_dataObject5)
158 _plot.AddDataObjectInput(_dataObject6)
159
160 # set the title and stuff
161 _plot.SetTitle("Example 2D plot with offsets")
162 _plot.SetXTitle("x")
163 _plot.SetYTitle("y")
164 _plot.SetXValuesToValue()
165
166 # set which parts of the data object are to be used for which axis
167 _plot.SetDataObjectXComponent(0,0)
168 _plot.SetDataObjectYComponent(0,1)
169 _plot.SetDataObjectYComponent(1,1)
170 _plot.SetDataObjectYComponent(2,1)
171 _plot.SetDataObjectYComponent(3,1)
172 _plot.SetDataObjectYComponent(4,1)
173 _plot.SetDataObjectYComponent(5,1)
174
175 # add the actor
176 _ren.AddActor2D(_plot)
177
178 # render the scene
179 _iren = vtk.vtkRenderWindowInteractor()
180 _iren.SetRenderWindow(_renWin)
181 _iren.Initialize()
182 _renWin.Render()
183 _iren.Start()
184
185 # convert the render window to an image
186 _win2imgFilter = vtk.vtkWindowToImageFilter()
187 _win2imgFilter.SetInput(_renWin)
188
189 # save the image to file
190 _outWriter = vtk.vtkPNGWriter()
191 _outWriter.SetInput(_win2imgFilter.GetOutput())
192 _outWriter.SetFileName("offsetLinePlot.png")
193 _outWriter.Write()
194
195 # pause for input
196 #raw_input('Press enter to continue...\n')
197
198 # vim: expandtab shiftwidth=4:

  ViewVC Help
Powered by ViewVC 1.1.26