/[escript]/trunk/pyvisi_old/examples/contourPlotExample.py
ViewVC logotype

Contents of /trunk/pyvisi_old/examples/contourPlotExample.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 820 - (show annotations)
Mon Aug 28 06:55:36 2006 UTC (13 years, 1 month ago) by gross
File MIME type: text/x-python
File size: 4804 byte(s)
make the modelframe test run
1 # $Id: contourPlotExample.py,v 1.8 2005/05/24 01:32:12 paultcochrane Exp $
2
3 """
4 Example of contour plotting with pyvisi
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 # what plotting method are we using?
26 method = 'vtk'
27
28 # set up some data to plot
29 # from Numeric import *
30 from numarray import *
31
32 # the x and y axes
33 # x = arange(-2,2,0.2, typecode=Float)
34 # y = arange(-2,3,0.2, typecode=Float)
35
36 x = arange(-2,2,0.2, type=Float)
37 y = arange(-2,3,0.2, type=Float)
38
39 # pick some interesting function to generate the data in the third dimension
40 # this is the one used in the matlab docs: z = x*exp(-x^2-y^2)
41 # z = zeros((len(x),len(y)), typecode=Float)
42 z = zeros((len(x),len(y)), type=Float)
43
44 # boy do *I* feel old fashioned writing it this way
45 # surely there's another way to do it: - something to do later
46 for i in range(len(x)):
47 for j in range(len(y)):
48 z[i,j] = x[i]*exp(-x[i]*x[i] - y[j]*y[j])
49
50 # plot it with either gnuplot, vtk or pyvisi
51 if method == 'pyvisi':
52 #### pyvisi version of code
53
54 # import the general pyvisi stuff
55 from esys.pyvisi import *
56 # import the gnuplot overrides of the interface
57 from esys.pyvisi.renderers.gnuplot import *
58 #from esys.pyvisi.renderers.vtk import *
59
60 # define a scene object
61 # a Scene is a container for all of the kinds of things you want to put
62 # into your plot, for instance, images, meshes, arrow/vector/quiver
63 # plots, contour plots, spheres etc.
64 scene = Scene()
65
66 # create a ContourPlot object
67 plot = ContourPlot(scene)
68
69 # add some helpful info to the plot
70 plot.title = 'Example contour plot'
71 plot.xlabel = 'x'
72 plot.ylabel = 'y'
73
74 # assign the data to the plot
75 # this version assumes that we have x, then y, then z and that z is 2D
76 # and that x and y are 1D arrays
77 plot.setData(x,y,z)
78 # alternative syntax
79 #plot.setData(xData=x, yData=y, zData=z)
80 # or (but more confusing depending upon one's naming conventions)
81 #plot.setData(x=x, y=y, z=z)
82
83 # render the scene to screen
84 scene.render(pause=True, interactive=True)
85
86 # save the scene to file
87 plot.setData(x,y,z) # have to do this now because we've already
88 # render()ed the scene. This requirement will be
89 # removed in the future
90 scene.save(fname="contourPlotExample.png", format=PngImage())
91
92 elif method == 'vtk':
93 #### original vtk code
94
95 import vtk
96
97 # set up the data
98 _points = vtk.vtkPoints()
99 _scalars = vtk.vtkFloatArray()
100
101 _index = 0
102 for _i in range(len(x)):
103 for _j in range(len(y)):
104 _points.InsertPoint(_index, x[_i], y[_j], 0)
105 _scalars.InsertValue(_index, z[_i,_j])
106
107 zMin = minval(minval(z))
108 zMax = maxval(maxval(z))
109
110 _data = vtk.vtkUnstructuredGrid()
111 _data.SetPoints(_points)
112 _data.GetPointData().SetScalars(_scalars);
113
114 # set up the contour
115 _plotContour = vtk.vtkContourGrid()
116 _plotContour.SetInput(_data)
117 _plotContour.GenerateValues(5, zMin, zMax)
118
119 # set up the mapper
120 _plotMapper = vtk.vtkPolyDataMapper()
121 _plotMapper.SetInput(_plotContour.GetOutput())
122 _plotMapper.SetScalarRange(zMin, zMax)
123
124 # set up the actor
125 _plotActor = vtk.vtkActor()
126 _plotActor.SetMapper(_plotMapper)
127
128 # use a scalar bar
129 #_scalarBar = vtk.vtkScalarBarActor()
130
131 #_lut = vtk.vtkLookupTable()
132 #_planeMapper = vtk.vtkPolyDataMapper()
133 #_planeMapper.SetInput(_data)
134 #_planeMapper.SetLookupTable(_lut)
135 #_planeMapper.SetScalarRange(0.0, 1.0)
136 #_scalarBarActor = vtk.vtkActor()
137 #_scalarBarActor.SetMapper(_planeMapper)
138
139 # set up the renderer and the render window
140 _ren = vtk.vtkRenderer()
141 _ren.SetBackground(1, 1, 1)
142 _renWin = vtk.vtkRenderWindow()
143 _renWin.AddRenderer(_ren)
144
145 # add the actor to the renderer
146 _ren.AddActor(_plotActor)
147 #_ren.AddActor(_scalarBarActor)
148
149 # render the scene
150 _iren = vtk.vtkRenderWindowInteractor()
151 _iren.SetRenderWindow(_renWin)
152 _iren.Initialize()
153 _renWin.Render()
154 _iren.Start()
155
156 #raw_input('Press enter to continue...')
157
158 elif method == 'plplot':
159 pass
160 else:
161 print "Eeek! What plotting method am I supposed to use???"
162
163 # vim: expandtab shiftwidth=4:

  ViewVC Help
Powered by ViewVC 1.1.26