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

Revision 337 - (show annotations)
Mon Dec 12 01:57:07 2005 UTC (14 years, 5 months ago) by cochrane
File MIME type: text/x-python
File size: 2189 byte(s)
```Initial merge of pyvisi into esys repository.
```
 1 # \$Id: scatterPlot3D.py,v 1.3 2005/11/08 08:23:45 paultcochrane Exp \$ 2 3 """ 4 Example of plotting 3D scatter plots with pyvisi 5 """ 6 7 import sys 8 numArgs = len(sys.argv) 9 if numArgs == 1: 10 ren_mod = "gnuplot" 11 else: 12 ren_mod = sys.argv[1] 13 14 # set up some data to plot 15 from Numeric import * 16 import random 17 18 # the x and y axes 19 x = arange(-2, 2, 0.2, typecode=Float) 20 y = arange(-3, 3, 0.2, typecode=Float) 21 22 # make the data a bit more scatter-like by using random numbers 23 random.seed() 24 for i in range(len(x)): 25 x[i] = x[i]*random.random() 26 27 for i in range(len(y)): 28 y[i] = y[i]*random.random() 29 30 # pick some interesting function to generate the data in the third dimension 31 # this is the one used in the matlab docs: z = x*exp(-x^2-y^2) 32 z = zeros((len(x),len(y)), typecode=Float) 33 34 # boy do *I* feel old fashioned writing it this way 35 # surely there's another way to do it: - something to do later 36 for i in range(len(x)): 37 for j in range(len(y)): 38 z[i,j] = exp(-x[i]*x[i] - y[j]*y[j]) 39 40 # import the general pyvisi stuff 41 from pyvisi import * 42 # import the renderer-specific overrides of the interface 43 if ren_mod == "gnuplot": 44 from pyvisi.renderers.gnuplot import * 45 elif ren_mod == "vtk": 46 from pyvisi.renderers.vtk import * 47 else: 48 raise ValueError, "Unknown renderer module" 49 50 # define a scene object 51 # a Scene is a container for all of the kinds of things you want to put 52 # into your plot, for instance, images, meshes, arrow/vector/quiver 53 # plots, contour plots, spheres etc. 54 scene = Scene() 55 56 # create a ScatterPlot3D object 57 plot = ScatterPlot3D(scene) 58 59 # add some helpful info to the plot 60 plot.title = 'Example 3D scatter plot' 61 plot.xlabel = 'x' 62 plot.ylabel = 'y' 63 plot.zlabel = 'z' 64 65 # assign the data to the plot 66 # this version assumes that we have x, then y, then z and that z is 2D 67 # and that x and y are 1D arrays 68 plot.setData(x,y,z) 69 # alternative syntax 70 #plot.setData(xData=x, yData=y, zData=z) 71 # or (but more confusing depending upon one's naming conventions) 72 #plot.setData(x=x, y=y, z=z) 73 74 # render the scene to screen 75 scene.render(pause=True, interactive=True) 76 77 # save the scene to file 78 scene.save(fname="scatterPlot3D.png", format=PngImage()) 79 80 # vim: expandtab shiftwidth=4: