[vtkusers] XY graph in VTK

Paul Cochrane paultcochrane at gmail.com
Fri Jun 9 05:49:18 EDT 2006


> I'm new to VTK and am trying to plot a simple 2D graph. I am using
> vtkXYPlotActor() and am trying to plot fielddata. I have managed to
> plot a single line, but am unsure of how to add another line to the
> plot (i.e. plot another set of data on the same set of axes). I would
> appreciate it If someone could help me with this.

There was a great post by Sander Niemeijer ages ago about how to plot
simple 2D graphs in vtk, and I've used that advice in a project of
mine (PyVisi; pyvisi.sf.net).  Here is an example from the PyVisi
distribution which plots multiple curves on the one set of axes, the
code is Python, but it shouldn't be too hard to convert to C++ or
other.  BTW: this code works with vtk 4.0, but probably not with vtk
5.0+ since the pipline model has changed.

I hope this helps!



code follows:

Example of plotting multiple curves with pyvisi

# set up some data to plot
from Numeric import *

x = arange(0, 2*pi, 0.1, typecode=Float)
y1 = sin(x)
y2 = cos(x)
y3 = cos(x)**2

#### original vtk code

import vtk

# set up the renderer and the render window
_ren = vtk.vtkRenderer()
_renWin = vtk.vtkRenderWindow()
_renWin.SetSize(640, 480)

# do a quick check to make sure x and y are same length
if len(x) != len(y1):
    raise ValueError, "x and y vectors must be same length"

if len(x) != len(y2):
    raise ValueError, "x and y vectors must be same length"

# set up the x and y data arrays to be able to accept the data (code
# here adapted from the C++ of a vtk-users mailing list reply by Sander
# Niemeijer)
_xData = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)

_yData1 = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)

_yData2 = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)

_yData3 = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)

# put the data into the data arrays
for i in range(len(x)):

# create a field data object
# (I think this is as a containter to hold the data arrays)
_fieldData1 = vtk.vtkFieldData()

_fieldData2 = vtk.vtkFieldData()

_fieldData3 = vtk.vtkFieldData()

# now put the field data object into a data object so that can add it as
# input to the xyPlotActor
_dataObject1 = vtk.vtkDataObject()

_dataObject2 = vtk.vtkDataObject()

_dataObject3 = vtk.vtkDataObject()

# set up the actor
_plot = vtk.vtkXYPlotActor()

# the size of the actor should be 80% of the render window
_plot.SetPosition(0.1, 0.1)  # (0.1 = (1.0 - 0.8)/2)

# set the title and stuff
_plot.SetTitle("Example 2D plot")
 # set which parts of the data object are to be used for which axis
_plot.SetDataObjectXComponent(0, 0)
_plot.SetDataObjectYComponent(0, 1)
_plot.SetDataObjectYComponent(1, 1)
_plot.SetDataObjectYComponent(2, 1)

# set up the lookup table for the appropriate range of colours
_lut = vtk.vtkLookupTable()
_colours = []
for i in range(3):

# change the colour of the separate lines
_plot.SetPlotColor(0, _colours[0][0], _colours[0][1], _colours[0][2])
_plot.SetPlotColor(1, _colours[1][0], _colours[1][1], _colours[1][2])
_plot.SetPlotColor(2, _colours[2][0], _colours[2][1], _colours[2][2])


# add the actor

# render the scene
_iren = vtk.vtkRenderWindowInteractor()

# convert the render window to an image
_win2imgFilter = vtk.vtkWindowToImageFilter()

# save the image to file
_outWriter = vtk.vtkPNGWriter()

# pause for input
raw_input('Press enter to continue...\n')

More information about the vtkusers mailing list