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

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

  ViewVC Help
Powered by ViewVC 1.1.26