New in ParaView: Coloring by string arrays
ParaView has always been able to map numerical values associated with points and cells to colors. ParaView 4.3 has the ability to map string arrays to colors. This feature works for string arrays in point data, cell data, and field data using the new feature that enables you to color by the first tuple in a field data array.
Coloring by string arrays is possible when the color map option “Interpret Values As Categories” is enabled and a set of annotations whose values are strings in the data array is defined. As with other cases where values are interpretted as categories, a set of active values can be obtained automatically from the data and defined as annotations using the tools in the Color Map Editor’s Annotations section.
Let’s take a look at an example data set that contains the countries of the world. The cells in the file that defined the countries have an associated string array named “REGION” that encodes the region in which the country is located. The image below shows the results of coloring by this region string array. Labels in the color legend show the region string values in the map data set.
The data file and ParaView state file that produced this image is attached to this post. You can try it out with ParaView 4.3 Release Candidate 1, available at the ParaView downloads page.
Mapping string arrays to colors in VTK
Coloring by string arrays is implemented in VTK, so any VTK-based application can do it. For example, imagine you have a data set where the colors to be used for cell data are defined by strings naming the colors. You can set up a vtkLookupTable to map these strings to the actual colors using the following code:
vtkLookupTable* lut = vtkLookupTable::New(); lut->IndexedLookupOn(); lut->SetNumberOfTableColors(5); lut->SetTableValue(0, 1.0, 0.0, 0.0); // red lut->SetTableValue(1, 0.0, 0.0, 1.0); // blue lut->SetTableValue(2, 0.0, 1.0, 0.0); // green lut->SetTableValue(3, 1.0, 1.0, 0.0); // yellow lut->SetTableValue(4, 0.0, 1.0, 1.0); // cyan vtkStdString red("red"); lut->SetAnnotation(red, red); vtkStdString blue("blue"); lut->SetAnnotation(blue, blue); vtkStdString green("green"); lut->SetAnnotation(green, green); vtkStdString yellow("yellow"); lut->SetAnnotation(yellow, yellow); vtkStdString cyan("cyan"); lut->SetAnnotation(cyan, cyan);
Happy color mapping!
Hi Cory,
In the post “New in ParaView: Coloring by string arrays”, I am not very clear about the process of mapping values to annotations using the tools in the Color Map Editor’s Annotations section. Was it done manually and then saved to the paraview state file?
Thanks
Wei
Hmm, it looks like the files in the post were lost during the transition from our old blog platform.
In any case, here are the steps in ParaView to color by a string array. You can do it all in the user interface, no programming needed.
– Load data into ParaView
– Select the string array in the Color By menu in the tool bar.
– Open the Color Map Editor
– Click the checkbox “Interpret Values as Categories” if it is not checked
– Under Annotations, click the button with the yellow-red-blue rectangle icon. This should add all the strings in your data as annotated values and the annotations themselves. You can then pick colors for each annotated value or apply a preset color map.
Hi Cory,
I was wondering if you could provide me with the sample data set that contains the countries of the world. Im working on a project and would really like to have that data.
Thanks!
Maria,
You can get the data file at http://www.vtk.org/files/ExternalData/MD5/ac98e21ef49c00040eda384905741d01.
Go to that link and then save the page with the name ‘political.vtp’.
HTH,
Cory