Use viscm to design color maps for ParaView 4.4
Update: Viridis and three other color maps with similar design parameters are now included in ParaView 5.0!
This week at SciPy2015 I attended the talk by Stéfan van der Walt and Nathaniel Smith on designing a better default color map for Python (you can view a recording of the talk here). matplotlib’s current default color map, shown below, is based on the rainbow color map, which has been shown to actively mislead viewers in their interpretation of data.
Smith and van der Walt’s talk laid out a few properties desired in a new default color map. It should be:
- Colorful
- Pretty
- Sequential
- Perceptually uniform in terms of uniform changes between successive colors in the map (even if printed in black and white)
- Accessible to color blind viewers (5-10% of males of European descent)
The proposed default color map, named viridis, looks like this:
This is a vast improvement over the jet colormap in terms of perceptual uniformity, accessibility to color blind viewers, and conversion to black and white.
But why should matplotlib have all the fun? Attached is a JSON file that can be imported into ParaView 4.4 (coming later this summer; bleeding edge nightly builds are available now) and later. To import this color map, open up the Color Map Editor and click on the “Choose preset” button. In the dialog that appears, choose the “Import” button, and select the file paraview_cmap.json. Viola! Viridis is now available in ParaView!
Update: Nicolás Guarín has converted the color map to the XML format (attached file) that ParaView versions prior to 4.4 can import.
Note that this color map is most appropriate for 2D data. Because the luminance of this color map changes from one end to the other, it is not recommended to color 3D surfaces because luminance changes in color maps interfere with surface shading from lighting.
To design this improved color map, van der Walt and Smith wrote a Python utility called viscm to design color maps with these properties. In addition to providing controls to define a color map in a way that has the desired properties, the utility shows the color map as printed in black and white and approximates how it appears to individuals with different types of color blindness. Screen shots of the utility are shown below.
A description of viscm and how to use it is available here. It provides the ability to export color maps in the form of a Python script. Exported color maps are saved in a file named /tmp/new_cm.py .
I have written a small Python script to convert this file to a JSON file that can be imported into the next release of ParaView. To use this script, first download the attached Python script named viscm2paraview.py. Then, run it with
python viscm2paraview.py /tmp/new_cm.py paraview_cmap.json "My Great Color Map"
Have fun trying viridis and designing new color maps for ParaView!
Hi Cory,
Great post – if other Paraview users want to try other Matplotlib color maps they are welcome to by adjusting the following code.
import numpy as np
import matplotlib.cm as cm
colors = cm.viridis(np.arange(256))
name = ‘viridis’
path = ‘viridis.xml’
def writePyPlotCMapToParaView(colors,name,path):
N = len(colors)
fid = open(path, ‘w’)
fid.write(”)
fid.write(‘\n’.format(name))
for i in range(N):
fid.write(‘\n’.format(i, colors[i,3], colors[i,0],colors[i,1],colors[i,2]))
fid.write(”)
fid.write(”)
fid.close()
Excellent! Thanks for the note, Gordon.
I should note that moving forward, ParaView color maps are now preferred to be exported and imported in JSON and eventually support for XML color maps will be removed.
This article states that viridis and other color maps are included in ParaView 5.0, but they are not available in the 5.1 version I have.
Hi Zach,
Which version and platform are you running? I just verified that Viridis and the other matplotlib color maps are available in ParaView 5.1 on Mac OS X.
Hi,
How do I get the attached json file? I don’t see attachments with the post?
Unfortunately, file attachments were lost from blog posts when we changed blogging platforms. The good news is that Viridis is now a preset color map available in ParaView since at least version 5.1, so you do not need to import the JSON file.