Memo‎ > ‎

My Python Porting of TestScatterPlotMatrix for VTK

posted Apr 12, 2015, 6:25 AM by Teng-Yok Lee   [ updated Apr 12, 2015, 6:26 AM ]
This code is based on the sample: https://github.com/Kitware/VTK/blob/master/Charts/Core/Testing/Cxx/TestScatterPlotMatrix.cxx

I modified it to python version and load the iris dataset as a demo.

import csv;

import numpy as np;

import vtk;

import vtk.util.numpy_support as VN;


# Load the iris dataset.

# NOTE: It is downloaded from:

# http://aima.cs.berkeley.edu/data/iris.csv


csv_filepath = r'F:\data\multivariate\iris\iris.csv';

n_cols = 4;

csv_table = np.loadtxt(open(csv_filepath, "rb"), delimiter=",", usecols=xrange(n_cols));

# Convert into the table format for VTK.

vtk_table = vtk.vtkTable();

for columni in range(n_cols):

    # Convert numpy array to vtk array.

    # Note: https://pyscience.wordpress.com/2014/09/06/numpy-to-vtk-converting-your-numpy-arrays-to-vtk-arrays-and-files/

    array = VN.numpy_to_vtk(np.ascontiguousarray(csv_table[:, columni]), deep=1);

    array.SetName("%d" % (columni))

    vtk_table.AddColumn(array);


######################################################################

# REF:    http://fossies.org/dox/ParaView-v4.1.0-source/TestScatterPlotMatrix_8cxx_source.html

matrix = vtk.vtkScatterPlotMatrix();

# Fine tune the color if needed.

# matrix.SetPlotColor(matrix.SCATTERPLOT, vtk.vtkColor4ub(0, 0, 0, 1));

# matrix.SetPlotColor(matrix.ACTIVEPLOT, vtk.vtkColor4ub(0, 0, 0, 1));

matrix_view = vtk.vtkContextView();

matrix_view.GetScene().AddItem(matrix);

matrix.SetInput(vtk_table);

matrix.SetNumberOfBins(7);

matrix_view.Render();

matrix_view.GetInteractor().Start()




Comments