[vtkusers] Simple plot question

Paul Cochrane cochrane at esscc.uq.edu.au
Tue Feb 22 19:38:55 EST 2005


Doug,

I'm a newbie to vtk too, but I've sort of worked out the xy-plot stuff
alright.  It doesn't seem that straightforward (to me) why one has to do so
many steps to be able to do something as "simple" as an xy plot but oh well.
I finally worked out how to do this by modifying the C++ example posted to
the vtkusers list by Sander Niemeijer (in the list archive).  The following
code is in python, but shouldn't be that hard to translate to Tcl or C++.

    import vtk

    from Numeric import *

    x = arange(10, typecode=Float)
    y = x**2

    # set up the renderer and the render window
    _ren = vtk.vtkRenderer()
    _renWin = vtk.vtkRenderWindow()
    _renWin.AddRenderer(_ren)

    # do a quick check to make sure x and y are same length
    if len(x) != len(y):
        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)
    _xData.SetNumberOfTuples(len(x))

    _yData = vtk.vtkDataArray.CreateDataArray(vtk.VTK_FLOAT)
    _yData.SetNumberOfTuples(len(y))

    # put the data into the data arrays
    for i in range(len(x)):
        _xData.SetTuple1(i,x[i])
        _yData.SetTuple1(i,y[i])

    # create a field data object 
    # (I think this is as a containter to hold the data arrays)
    _fieldData = vtk.vtkFieldData()
    _fieldData.AllocateArrays(2)
    _fieldData.AddArray(_xData)
    _fieldData.AddArray(_yData)

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

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

    # set the title and stuff
    _plot.SetTitle("Example 2D plot")
    _plot.SetXTitle("x")
    _plot.SetYTitle("x^2")
    _plot.SetXValuesToValue()

    # set which parts of the data object are to be used for which axis
    _plot.SetDataObjectXComponent(0,0)
    _plot.SetDataObjectYComponent(0,1)

    # add the actor
    _ren.AddActor2D(_plot)

    # render the scene
    _iren = vtk.vtkRenderWindowInteractor()
    _iren.SetRenderWindow(_renWin)
    _iren.Initialize()
    _renWin.Render()
    _iren.Start()

Just so you know, I embed this code in something else, so that's why I use
all of the leading underscores.  

What I think is going on is that vtk needs data packaged in a particular
form, and there are a few hoops one must go through to get it into that form
so that one can pass it off to the vtkActor object.  Going through the code,
I set up the renderer and render window at the start rather than the end
(dunno why, but must have seemed like a good idea at the time).  I do a
check on the x and y variables I'm passing into the script to make sure the
dimensions are correct (this is a simple plot of y = x^2).  One then needs
to generate vtkDataArray objects to hold the x and y data, telling vtk the
number of tuples allocates the relevant amount of memory.  Then using
SetTuple1() (for some reason SetTuple() doesn't work in vtk python) to
assign the elements of the x and y arrays to the vtkDataArrays.  The
vtkDataArrays then need to be packaged up as a vtkFieldData object with two
sub arrays.  The vtkFieldData object is then passed to a vtkDataObject which
can then be used as input to a vtkXYPlotActor.  Then you have to set which
component of the vtkDataObject to use as the X or Y component, and the rest
of the code is just setting stuff up, adding the plot actor to the renderer,
and rendering stuff in an interactive window.

This has been more verbose than I intended, but I hope it has been of some
help.

Regards,

Paul

* Doug Henry (brilligent at gmail.com) [050223 02:59]:
> This is hopefully an easy "newbie" type question.  I was looking at
> making a simple xy-plot as a way to figure out the basics of vtk (I'm
> going to wait for my VTK textbook to arrive before diving in with both
> feet).  I have worked through the cone tutorials and created my own
> interactor for my toolkit, so I have some familiarity with the vtk
> toolkit.  Looking at the xyPlot tcl example, it is not obvious to me
> how data is presented to xyplot.  I was hoping someone could give me a
> starting point by explaining how I would simply plot two columns of
> numbers against each other.  This should give me a simple example of
> how this data is mapped, and hopefully help me to better understand
> the probe stuff in the tcl example.
> 
> Thanks for any help,
> Doug
> _______________________________________________
> This is the private VTK discussion list. 
> Please keep messages on-topic. Check the FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers

-- 
Paul Cochrane
Computational Scientist/Software Developer
Earth Systems Science Computational Centre
University of Queensland
Brisbane
Queensland 4072
Australia

E: cochrane at esscc dot uq dot edu dot au



More information about the vtkusers mailing list