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

Annotation of /trunk/pyvisi/examples/simplePlotMovieExample.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 637 - (hide annotations)
Thu Mar 23 10:55:31 2006 UTC (14 years, 3 months ago) by gross
File MIME type: text/x-python
File size: 5204 byte(s)
more copyright statements added
1 cochrane 337 # $Id: simplePlotMovieExample.py,v 1.2 2005/03/07 04:17:40 paultcochrane Exp $
2    
3     """
4     Example of plotting a changing function with pyvisi
5 gross 637
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 cochrane 337 """
13    
14 gross 637 __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 cochrane 337 # what plotting method are we using?
26     method = 'pyvisi'
27    
28     # set up some data to plot
29     from Numeric import *
30    
31     x = arange(10, typecode=Float)
32     y = x**2
33    
34     # plot it using one of the three methods
35     if method == 'pyvisi':
36    
37     # example code for how a user would write a script in pyvisi
38 gross 605 from esys.pyvisi import * # base level visualisation stuff
39     #from esys.pyvisi.utils import * # pyvisi specific utils
40 cochrane 337 # import the objects to render the scene using the specific renderer
41 gross 605 from esys.pyvisi.renderers.gnuplot import * # gnuplot
42     #from esys.pyvisi.renderers.vtk import * # vtk
43 cochrane 337
44     # define the scene object
45     # a Scene is a container for all of the kinds of things you want to put
46     # into your plot for instance, images, meshes, arrow/vector/quiver plots,
47     # contour plots, spheres etc.
48     scene = Scene()
49    
50     # create a LinePlot object
51     plot = LinePlot(scene)
52    
53     # add some helpful info to the plot
54     plot.title = 'Example 2D plot'
55     plot.xlabel = 'x'
56     plot.ylabel = 'x^2'
57    
58     plot.linestyle = 'lines'
59    
60     for i in range(100):
61     # assign some data to the plot
62     plot.setData(x, y)
63    
64     # render the scene to screen
65     #scene.render(pause=True, interactive=True)
66    
67     # save the scene out to file
68     scene.save(fname="simplePlotMovieExample%03d.png"%i, format=PngImage())
69     #scene.save(fname="simplePlotMovieExample.ps", format=PsImage())
70     y = y*0.9
71    
72     elif method == 'gnuplot':
73     #### original gnuplot code
74    
75     import Gnuplot
76    
77     # set the plot up
78     _gnuplot = Gnuplot.Gnuplot()
79     _gnuplot.title('Example 2D plot')
80     _gnuplot.xlabel('x')
81     _gnuplot.ylabel('x^2')
82    
83     for i in range(100):
84     # set up the data
85     _data = Gnuplot.Data(x, y, with='lines')
86    
87     # set up to save to file
88     _gnuplot('set terminal png')
89     _gnuplot('set output \"simplePlotMovieExample%03d.png\"'%i)
90    
91     # save it
92     _gnuplot.plot(_data)
93    
94     y = y*0.9
95    
96     raw_input('Press enter to continue...\n')
97    
98     elif method == 'vtk':
99     #### original vtk code
100    
101     import vtk
102    
103     # set up the renderer and the render window
104     _ren = vtk.vtkRenderer()
105     _renWin = vtk.vtkRenderWindow()
106     _renWin.AddRenderer(_ren)
107    
108     # do a quick check to make sure x and y are same length
109     if len(x) != len(y):
110     raise ValueError, "x and y vectors must be same length"
111    
112     # set up the x and y data arrays to be able to accept the data (code
113     # here adapted from the C++ of a vtk-users mailing list reply by Sander
114     # Niemeijer)
115     _xData = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
116     _xData.SetNumberOfTuples(len(x))
117    
118     _yData = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
119     _yData.SetNumberOfTuples(len(y))
120    
121     # put the data into the data arrays
122     for i in range(len(x)):
123     _xData.SetTuple1(i,x[i])
124     _yData.SetTuple1(i,y[i])
125    
126     # create a field data object
127     # (I think this is as a containter to hold the data arrays)
128     _fieldData = vtk.vtkFieldData()
129     _fieldData.AllocateArrays(2)
130     _fieldData.AddArray(_xData)
131     _fieldData.AddArray(_yData)
132    
133     # now put the field data object into a data object so that can add it as
134     # input to the xyPlotActor
135     _dataObject = vtk.vtkDataObject()
136     _dataObject.SetFieldData(_fieldData)
137    
138     # set up the actor
139     _plot = vtk.vtkXYPlotActor()
140     _plot.AddDataObjectInput(_dataObject)
141    
142     # set the title and stuff
143     _plot.SetTitle("Example 2D plot")
144     _plot.SetXTitle("x")
145     _plot.SetYTitle("x^2")
146     _plot.SetXValuesToValue()
147    
148     # set which parts of the data object are to be used for which axis
149     _plot.SetDataObjectXComponent(0,0)
150     _plot.SetDataObjectYComponent(0,1)
151    
152     # add the actor
153     _ren.AddActor2D(_plot)
154    
155     # render the scene
156     _iren = vtk.vtkRenderWindowInteractor()
157     _iren.SetRenderWindow(_renWin)
158     _iren.Initialize()
159     _renWin.Render()
160     _iren.Start()
161    
162     # convert the render window to an image
163     _win2imgFilter = vtk.vtkWindowToImageFilter()
164     _win2imgFilter.SetInput(_renWin)
165    
166     # save the image to file
167     _outWriter = vtk.vtkPNGWriter()
168     _outWriter.SetInput(_win2imgFilter.GetOutput())
169     _outWriter.SetFileName("simplePlotExample.png")
170     _outWriter.Write()
171    
172     # pause for input
173     #raw_input('Press enter to continue...\n')
174    
175     else:
176     print "Eeek! What plotting method am I supposed to use???"
177    
178     # vim: expandtab shiftwidth=4:
179    

  ViewVC Help
Powered by ViewVC 1.1.26