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

Contents of /trunk/pyvisi/examples/plotExample.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: 5316 byte(s)
pyvisi with vtk runs noe on the altix
1 # Copyright (C) 2004 Paul Cochrane
2 #
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
17 # $Id: plotExample.py,v 1.14 2005/02/07 08:17:03 paultcochrane Exp $
18
19 ## @file plotExample.py
20
21 """
22 Example of plotting with pyvisi
23
24 Will hopefully help me write a decent interface.
25 """
26
27 import sys
28 sys.path.append('../')
29
30 # what plotting method are we using?
31 method = 'pyvisi'
32
33 # set up some data to plot
34 from Numeric import *
35
36 x = arange(10, typecode=Float)
37 y = x**2
38
39 # plot it using one of the three methods
40 if method == 'pyvisi':
41
42 # example code for how a user would write a script in pyvisi
43 from esys.pyvisi import * # base level visualisation stuff
44 #from esys.pyvisi.utils import * # pyvisi specific utils
45 # import the objects to render the scene using the specific renderer
46 from esys.pyvisi.renderers.gnuplot import * # gnuplot
47 #from esys.pyvisi.renderers.vtk import * # vtk
48
49 # define the scene object
50 # a Scene is a container for all of the kinds of things you want to put
51 # into your plot for instance, images, meshes, arrow/vector/quiver plots,
52 # contour plots, spheres etc.
53 scene = Scene()
54
55 # create a LinePlot object
56 plot = LinePlot(scene)
57
58 # add some helpful info to the plot
59 plot.title = 'Example 2D plot'
60 plot.xlabel = 'x'
61 plot.ylabel = 'x^2'
62
63 plot.linestyle = 'lines'
64
65 # assign some data to the plot
66 plot.setData(x,y)
67 plot.render() # need to tell some renderers to finish up stuff here
68
69 # render the scene to screen
70 scene.render(pause=True,interactive=True)
71
72 # save the scene out to file
73 scene.save(fname="plotExample.png", format=PngImage())
74 scene.save(fname="plotExample.ps", format=PsImage())
75
76 elif method == 'gnuplot':
77 #### original gnuplot code
78
79 import Gnuplot
80
81 # set the plot up
82 _gnuplot = Gnuplot.Gnuplot()
83 _gnuplot.title('Example 2D plot')
84 _gnuplot.xlabel('x')
85 _gnuplot.ylabel('x^2')
86
87 # set up the data
88 _data = Gnuplot.Data(x, y, with='lines')
89
90 # plot it
91 _gnuplot.plot(_data)
92
93 # set up to save to file
94 _gnuplot('set terminal png')
95 _gnuplot('set output \"plotExample.png\"')
96
97 # save it
98 _gnuplot.plot(_data)
99
100 raw_input('Press enter to continue...\n')
101
102 elif method == 'vtk':
103 #### original vtk code
104
105 import vtk
106
107 # set up the renderer and the render window
108 _ren = vtk.vtkRenderer()
109 _renWin = vtk.vtkRenderWindow()
110 _renWin.AddRenderer(_ren)
111
112 # do a quick check to make sure x and y are same length
113 if len(x) != len(y):
114 raise ValueError, "x and y vectors must be same length"
115
116 # set up the x and y data arrays to be able to accept the data (code
117 # here adapted from the C++ of a vtk-users mailing list reply by Sander
118 # Niemeijer)
119 _xData = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
120 _xData.SetNumberOfTuples(len(x))
121
122 _yData = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
123 _yData.SetNumberOfTuples(len(y))
124
125 # put the data into the data arrays
126 for i in range(len(x)):
127 _xData.SetTuple1(i,x[i])
128 _yData.SetTuple1(i,y[i])
129
130 # create a field data object
131 # (I think this is as a containter to hold the data arrays)
132 _fieldData = vtk.vtkFieldData()
133 _fieldData.AllocateArrays(2)
134 _fieldData.AddArray(_xData)
135 _fieldData.AddArray(_yData)
136
137 # now put the field data object into a data object so that can add it as
138 # input to the xyPlotActor
139 _dataObject = vtk.vtkDataObject()
140 _dataObject.SetFieldData(_fieldData)
141
142 # set up the actor
143 _plot = vtk.vtkXYPlotActor()
144 _plot.AddDataObjectInput(_dataObject)
145
146 # set the title and stuff
147 _plot.SetTitle("Example 2D plot")
148 _plot.SetXTitle("x")
149 _plot.SetYTitle("x^2")
150 _plot.SetXValuesToValue()
151
152 # set which parts of the data object are to be used for which axis
153 _plot.SetDataObjectXComponent(0,0)
154 _plot.SetDataObjectYComponent(0,1)
155
156 # add the actor
157 _ren.AddActor2D(_plot)
158
159 # render the scene
160 _iren = vtk.vtkRenderWindowInteractor()
161 _iren.SetRenderWindow(_renWin)
162 _iren.Initialize()
163 _renWin.Render()
164 _iren.Start()
165
166 # convert the render window to an image
167 _win2imgFilter = vtk.vtkWindowToImageFilter()
168 _win2imgFilter.SetInput(_renWin)
169
170 # save the image to file
171 _outWriter = vtk.vtkPNGWriter()
172 _outWriter.SetInput(_win2imgFilter.GetOutput())
173 _outWriter.SetFileName("plotExample.png")
174 _outWriter.Write()
175
176 # pause for input
177 #raw_input('Press enter to continue...\n')
178
179 else:
180 print "Eeek! What plotting method am I supposed to use???"
181
182 # vim: expandtab shiftwidth=4:
183

  ViewVC Help
Powered by ViewVC 1.1.26