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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26