/[escript]/trunk/pyvisi/test/python/vtk/simpleLinePlot.py
ViewVC logotype

Contents of /trunk/pyvisi/test/python/vtk/simpleLinePlot.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 604 - (show annotations)
Mon Mar 20 02:23:14 2006 UTC (13 years, 10 months ago) by gross
File MIME type: text/x-python
File size: 2562 byte(s)
example cleanup
1 # $Id: simpleLinePlot.py,v 1.1 2005/05/05 01:57:53 paultcochrane Exp $
2
3 """
4 Example of plotting lines with pyvisi
5
6 This is the original code used to develop the vtk renderer module
7 """
8
9 # set up some data to plot
10 from Numeric import *
11
12 x = arange(10, typecode=Float)
13 y = x**2
14
15 import vtk
16
17 # do a quick check to make sure x and y are same length
18 if len(x) != len(y):
19 raise ValueError, "x and y vectors must be same length"
20
21 # set up the x and y data arrays to be able to accept the data (code
22 # here adapted from the C++ of a vtk-users mailing list reply by Sander
23 # Niemeijer)
24 _xData = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
25 _xData.SetNumberOfTuples(len(x))
26
27 _yData = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
28 _yData.SetNumberOfTuples(len(y))
29
30 # put the data into the data arrays
31 for i in range(len(x)):
32 _xData.SetTuple1(i,x[i])
33 _yData.SetTuple1(i,y[i])
34
35 # create a field data object
36 # (I think this is as a containter to hold the data arrays)
37 _fieldData = vtk.vtkFieldData()
38 _fieldData.AllocateArrays(2)
39 _fieldData.AddArray(_xData)
40 _fieldData.AddArray(_yData)
41
42 # now put the field data object into a data object so that can add it as
43 # input to the xyPlotActor
44 _dataObject = vtk.vtkDataObject()
45 _dataObject.SetFieldData(_fieldData)
46
47 # set up the actor
48 _plot = vtk.vtkXYPlotActor()
49 _plot.AddDataObjectInput(_dataObject)
50
51 # set the title and stuff
52 _plot.SetTitle("Example 2D plot")
53 _plot.SetXTitle("x")
54 _plot.SetYTitle("x^2")
55 _plot.SetXValuesToValue()
56
57 # set which parts of the data object are to be used for which axis
58 _plot.SetDataObjectXComponent(0, 0)
59 _plot.SetDataObjectYComponent(0, 1)
60
61 # give the plot some nicer colours
62 _plot.SetPlotColor(0, 1.0, 0.0, 0.0)
63
64 # set up the renderer and the render window
65 _ren = vtk.vtkRenderer()
66 _renWin = vtk.vtkRenderWindow()
67 _renWin.AddRenderer(_ren)
68 _renWin.SetSize(640, 480)
69
70 # make the colours a bit nicer of the whole plot
71 _plot.GetXAxisActor2D().GetProperty().SetColor(0,0,0)
72 _plot.GetYAxisActor2D().GetProperty().SetColor(0,0,0)
73 _ren.SetBackground(1,1,1)
74
75 # add the actor
76 _ren.AddActor2D(_plot)
77
78 # render the scene
79 _iren = vtk.vtkRenderWindowInteractor()
80 _iren.SetRenderWindow(_renWin)
81 _iren.Initialize()
82 _renWin.Render()
83 _iren.Start()
84
85 # convert the render window to an image
86 _win2imgFilter = vtk.vtkWindowToImageFilter()
87 _win2imgFilter.SetInput(_renWin)
88
89 # save the image to file
90 _outWriter = vtk.vtkPNGWriter()
91 _outWriter.SetInput(_win2imgFilter.GetOutput())
92 _outWriter.SetFileName("simpleLinePlot.png")
93 _outWriter.Write()
94
95 # pause for input
96 raw_input('Press enter to continue...\n')

  ViewVC Help
Powered by ViewVC 1.1.26