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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 637 - (show annotations)
Thu Mar 23 10:55:31 2006 UTC (14 years, 2 months ago) by gross
File MIME type: text/x-python
File size: 5088 byte(s)
more copyright statements added
1 """
2 Example of plotting with pyvisi
3
4 Will hopefully help me write a decent interface.
5
6 @var __author__: name of author
7 @var __license__: licence agreement
8 @var __copyright__: copyrights
9 @var __url__: url entry point on documentation
10 @var __version__: version
11 @var __date__: date of the version
12 """
13
14 __copyright__=""" Copyright (c) 2006 by ACcESS MNRF
15 http://www.access.edu.au
16 Primary Business: Queensland, Australia"""
17 __license__="""Licensed under the Open Software License version 3.0
18 http://www.opensource.org/licenses/osl-3.0.php"""
19 __author__="Paul Cochrane"
20 __url__="http://www.iservo.edu.au/esys"
21 __version__="$Revision$"
22 __date__="$Date$"
23
24
25 import sys
26 sys.path.append('../')
27
28 # what plotting method are we using?
29 method = 'pyvisi'
30
31 # set up some data to plot
32 from Numeric import *
33
34 x = arange(10, typecode=Float)
35 y = x**2
36
37 # plot it using one of the three methods
38 if method == 'pyvisi':
39
40 # example code for how a user would write a script in pyvisi
41 from esys.pyvisi import * # base level visualisation stuff
42 #from esys.pyvisi.utils import * # pyvisi specific utils
43 # import the objects to render the scene using the specific renderer
44 from esys.pyvisi.renderers.gnuplot import * # gnuplot
45 #from esys.pyvisi.renderers.vtk import * # vtk
46
47 # define the scene object
48 # a Scene is a container for all of the kinds of things you want to put
49 # into your plot for instance, images, meshes, arrow/vector/quiver plots,
50 # contour plots, spheres etc.
51 scene = Scene()
52
53 # create a LinePlot object
54 plot = LinePlot(scene)
55
56 # add some helpful info to the plot
57 plot.title = 'Example 2D plot'
58 plot.xlabel = 'x'
59 plot.ylabel = 'x^2'
60
61 plot.linestyle = 'lines'
62
63 # assign some data to the plot
64 plot.setData(x,y)
65 plot.render() # need to tell some renderers to finish up stuff here
66
67 # render the scene to screen
68 scene.render(pause=True,interactive=True)
69
70 # save the scene out to file
71 scene.save(fname="plotExample.png", format=PngImage())
72 scene.save(fname="plotExample.ps", format=PsImage())
73
74 elif method == 'gnuplot':
75 #### original gnuplot code
76
77 import Gnuplot
78
79 # set the plot up
80 _gnuplot = Gnuplot.Gnuplot()
81 _gnuplot.title('Example 2D plot')
82 _gnuplot.xlabel('x')
83 _gnuplot.ylabel('x^2')
84
85 # set up the data
86 _data = Gnuplot.Data(x, y, with='lines')
87
88 # plot it
89 _gnuplot.plot(_data)
90
91 # set up to save to file
92 _gnuplot('set terminal png')
93 _gnuplot('set output \"plotExample.png\"')
94
95 # save it
96 _gnuplot.plot(_data)
97
98 raw_input('Press enter to continue...\n')
99
100 elif method == 'vtk':
101 #### original vtk code
102
103 import vtk
104
105 # set up the renderer and the render window
106 _ren = vtk.vtkRenderer()
107 _renWin = vtk.vtkRenderWindow()
108 _renWin.AddRenderer(_ren)
109
110 # do a quick check to make sure x and y are same length
111 if len(x) != len(y):
112 raise ValueError, "x and y vectors must be same length"
113
114 # set up the x and y data arrays to be able to accept the data (code
115 # here adapted from the C++ of a vtk-users mailing list reply by Sander
116 # Niemeijer)
117 _xData = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
118 _xData.SetNumberOfTuples(len(x))
119
120 _yData = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
121 _yData.SetNumberOfTuples(len(y))
122
123 # put the data into the data arrays
124 for i in range(len(x)):
125 _xData.SetTuple1(i,x[i])
126 _yData.SetTuple1(i,y[i])
127
128 # create a field data object
129 # (I think this is as a containter to hold the data arrays)
130 _fieldData = vtk.vtkFieldData()
131 _fieldData.AllocateArrays(2)
132 _fieldData.AddArray(_xData)
133 _fieldData.AddArray(_yData)
134
135 # now put the field data object into a data object so that can add it as
136 # input to the xyPlotActor
137 _dataObject = vtk.vtkDataObject()
138 _dataObject.SetFieldData(_fieldData)
139
140 # set up the actor
141 _plot = vtk.vtkXYPlotActor()
142 _plot.AddDataObjectInput(_dataObject)
143
144 # set the title and stuff
145 _plot.SetTitle("Example 2D plot")
146 _plot.SetXTitle("x")
147 _plot.SetYTitle("x^2")
148 _plot.SetXValuesToValue()
149
150 # set which parts of the data object are to be used for which axis
151 _plot.SetDataObjectXComponent(0,0)
152 _plot.SetDataObjectYComponent(0,1)
153
154 # add the actor
155 _ren.AddActor2D(_plot)
156
157 # render the scene
158 _iren = vtk.vtkRenderWindowInteractor()
159 _iren.SetRenderWindow(_renWin)
160 _iren.Initialize()
161 _renWin.Render()
162 _iren.Start()
163
164 # convert the render window to an image
165 _win2imgFilter = vtk.vtkWindowToImageFilter()
166 _win2imgFilter.SetInput(_renWin)
167
168 # save the image to file
169 _outWriter = vtk.vtkPNGWriter()
170 _outWriter.SetInput(_win2imgFilter.GetOutput())
171 _outWriter.SetFileName("plotExample.png")
172 _outWriter.Write()
173
174 # pause for input
175 #raw_input('Press enter to continue...\n')
176
177 else:
178 print "Eeek! What plotting method am I supposed to use???"
179
180 # vim: expandtab shiftwidth=4:
181

  ViewVC Help
Powered by ViewVC 1.1.26