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

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

  ViewVC Help
Powered by ViewVC 1.1.26