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

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

  ViewVC Help
Powered by ViewVC 1.1.26