Take the 2-minute tour ×

I would like to use Visualisation Toolkit to scatter plot points, each of which will have a different colour. I have used the advice given here to plot points in a gray colour, but am failing to understand how to give a colour to each one.

The relevant parts of the cube example are:

vtkPolyData *cube = vtkPolyData::New();
vtkPoints *points = vtkPoints::New();
vtkCellArray *polys = vtkCellArray::New();
vtkFloatArray *scalars = vtkFloatArray::New();

// Load the point, cell, and data attributes.
for (i=0; i<8; i++) points->InsertPoint(i,x[i]);
for (i=0; i<6; i++) polys->InsertNextCell(4,pts[i]);
for (i=0; i<8; i++) scalars->InsertTuple1(i,i);

// We now assign the pieces to the vtkPolyData.

How can I give each of the Verts a colour?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

I found a basic tutorial for what I was trying to do. This shows how to add colours for each point:


The relevant lines are as follows:

// Setup colors
vtkSmartPointer<vtkUnsignedCharArray> colors =
colors->SetName ("Colors");
  for (int i = 0; i < nV; ++i)
    unsigned char tempColor[3] = {(int)c[i],
    colors->InsertNextTupleValue (tempColor);

share|improve this answer
add comment

You may be interested in the new 2d plotting infrastructure in VTK. See example here: http://www.vtk.org/Wiki/VTK/Examples/Cxx/Plotting/LinePlot

Also see this discussion of plotting in 3D: http://www.kitware.com/source/home/post/40

share|improve this answer
add comment

Your Answer


