# Contents of /trunk/pyvisi/examples/contourPlotMovieExample.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: 2959 byte(s)
Initial merge of pyvisi into esys repository.
 1 # \$Id: contourPlotMovieExample.py,v 1.3 2005/03/07 05:23:37 paultcochrane Exp \$ 2 3 """ 4 Example of contour plotting a changing function 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 36 # define a scene object 37 # a Scene is a container for all of the kinds of things you want to put 38 # into your plot, for instance, images, meshes, arrow/vector/quiver 39 # plots, contour plots, spheres etc. 40 scene = Scene() 41 42 # create a ContourPlot object 43 plot = ContourPlot(scene) 44 45 # add some helpful info to the plot 46 plot.title = 'Example contour plot' 47 plot.xlabel = 'x' 48 plot.ylabel = 'y' 49 50 for i in range(100): 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 # save the scene to file 61 scene.save(fname="contourPlotMovieExample%03d.png"%i, format=PngImage()) 62 63 # update the value of z to plot 64 z = z*0.9 65 66 elif method == 'gnuplot': 67 #### original gnuplot code 68 69 import Gnuplot 70 71 # set the plot up 72 _gnuplot = Gnuplot.Gnuplot() 73 _gnuplot.title('Example contour plot') 74 _gnuplot.xlabel('x') 75 _gnuplot.ylabel('y') 76 #_gnuplot.zlabel('z') 77 78 # this is a contour plot, so... 79 _gnuplot('set contour base') 80 _gnuplot('set view 0, 0, 1, 1') 81 _gnuplot('set nosurface') 82 _gnuplot('set terminal png') 83 84 for i in range(100): 85 plotString = "set output \"contourPlotMovieExample%03d.png\""%i 86 _gnuplot(plotString) 87 # set up the data 88 _data = Gnuplot.GridData(z,x,y, binary=1) 89 90 _gnuplot.splot(_data) 91 92 # update the value of z to plot 93 z = z*0.9 94 95 raw_input('Press enter to continue...') 96 97 elif method == 'vtk': 98 print "vtk contour plotting not yet implemented" 99 100 else: 101 print "Eeek! What plotting method am I supposed to use???" 102 103 # vim: expandtab shiftwidth=4: