# Contents of /trunk/pyvisi/examples/surfaceAndContourPlotExample.py

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: 3322 byte(s)
```Initial merge of pyvisi into esys repository.
```
 1 # \$Id: surfaceAndContourPlotExample.py,v 1.4 2005/03/07 05:23:37 paultcochrane Exp \$ 2 3 """ 4 Example of plotting surfaces with contours in pyvisi 5 6 Will hopefully help me write a decent interface. 7 """ 8 9 # what plotting method are we using? 10 method = 'pyvisi' 11 12 # set up some data to plot 13 from Numeric import * 14 15 # the x and y axes 16 x = arange(-2,2,0.2, typecode=Float) 17 y = arange(-2,3,0.2, typecode=Float) 18 19 # pick some interesting function to generate the data in the third dimension 20 # this is the one used in the matlab docs: z = x*exp(-x^2-y^2) 21 z = zeros((len(x),len(y)), typecode=Float) 22 23 # boy do *I* feel old fashioned writing it this way 24 # surely there's another way to do it: - something to do later 25 for i in range(len(x)): 26 for j in range(len(y)): 27 z[i,j] = x[i]*exp(-x[i]*x[i] - y[j]*y[j]) 28 29 # plot it with either gnuplot, vtk or pyvisi 30 if method == 'pyvisi': 31 #### pyvisi version of code 32 33 # import the general pyvisi stuff 34 from pyvisi import * 35 # import the gnuplot overrides of the interface 36 from pyvisi.renderers.gnuplot import * 37 38 # define a scene object 39 # a Scene is a container for all of the kinds of things you want to put 40 # into your plot, for instance, images, meshes, arrow/vector/quiver 41 # plots, contour plots, spheres etc. 42 scene = Scene() 43 44 # create a SurfacePlot object 45 plot = SurfacePlot(scene) 46 47 # add some helpful info to the plot 48 plot.title = 'Example surface plot with contour on base' 49 plot.xlabel = 'x' 50 plot.ylabel = 'y' 51 plot.zlabel = 'z' 52 53 # add a contour to the base of the axes 54 plot.contours = True 55 56 # assign the data to the plot 57 # this version assumes that we have x, then y, then z and that z is 2D 58 # and that x and y are 1D arrays 59 plot.setData(x,y,z) 60 # alternative syntax 61 #plot.setData(xData=x, yData=y, zData=z) 62 # or (but more confusing depending upon one's naming conventions) 63 #plot.setData(x=x, y=y, z=z) 64 65 # render the scene to screen 66 scene.render(pause=True, interactive=True) 67 68 # save the scene to file 69 plot.setData(x,y,z) # have to do this now because we've already 70 # render()ed the scene. This requirement will be 71 # removed in the future 72 scene.save(fname="surfaceAndContourPlotExample.png", format=PngImage()) 73 plot.setData(x,y,z) # have to do this now because we've already save()d 74 # the scene. This requirement will be removed in 75 # the future 76 scene.save(fname="surfaceAndContourPlotExample.ps", format=PsImage()) 77 78 elif method == 'gnuplot': 79 #### original gnuplot code 80 81 import Gnuplot 82 83 # set the plot up 84 _gnuplot = Gnuplot.Gnuplot() 85 _gnuplot.title('Example surface plot with contour on base') 86 _gnuplot.xlabel('x') 87 _gnuplot.ylabel('y') 88 _gnuplot('set zlabel \'z\'') 89 90 # this is a surface plot, so... 91 _gnuplot('set surface') 92 _gnuplot('set data style lines') 93 _gnuplot('set contour base') 94 95 # set up the data 96 _data = Gnuplot.GridData(z,x,y, binary=1) 97 98 _gnuplot.splot(_data) 99 100 raw_input('Press enter to continue...') 101 102 elif method == 'vtk': 103 print "vtk surface plotting not yet implemented" 104 105 else: 106 print "Eeek! What plotting method am I supposed to use???" 107 108 # vim: expandtab shiftwidth=4: