A brief example using this colormap and the colormap conversion script for MATLAB and Matplotlib users

First, you will need a colormap. This example will use the colormap above, FloatPNG_PV44.xml, which is the colormap needed to create ColorMoves compatible images. To download FloatPNG_PV44 click the colormap above. Any of the colormaps found here can also be used.

MATLAB Users

Run the script with the following options:

To get a MATLAB compatible matrix of RGB values, run the script.

python cm_xml_to_matplotlib.py -m -f /path/to/xmlcolormap.xml

The script will output a large matrix like below and a plot of the colormap.

matlab-colormap = [ 0.0, 0.0, 0.0040000000000000001 
0.0040000000000000001, 0.0039521097578665227, 0.0 
0.0080000000000000002, 0.007889056287435435, 0.0 
0.012, 0.011825748852398326, 0.0 
...
0.96899999999999997, 0.97241165759070169, 0.0 
0.97299999999999998, 0.97634834711642648, 0.0 
0.97599999999999998, 0.98028529364599548, 0.0 
0.97999999999999998, 0.98422223612061754, 0.0 
0.98399999999999999, 0.98815892970123198, 0.0 
0.98799999999999999, 0.99209587419941025, 0.0 
0.99199999999999999, 0.99603256575649723, 0.0 
0.996, 0.99996951227823061, 0.0 
1.0, 1.0, 1.0 ];

The matrix is very long, so this is only a portion of it. Copy the matrix, and paste it into your MATLAB program. To use the colormap on the current plot in MATLAB:

colormap(matlab-colormap)

To use the colormap on a plot other than the current plot:

colormap(target-plot, matlab-colormap)

Matplotlib Users

This script can be imported as a module. Save cm_xml_to_matplotlib.py in the same location as the script you would like to use and import:

import cm_xml_to_matplotlib as cm

Functions:

mycmap = cm.make_cmap('Your_Colormap_Here.xml') #make the Matplotlib compatible colormap
## to use colormap: matplotlib.pyplot.imshow(your_image, cmap=matplotlib.pyplot.get_cmap(mycmap))
cm.cmap_matrix(mycmap) #output the MATLAB compatible matrix
cm.plot_cmap(mycmap) #plot an 8 by 1 copy of the colormap

Source code:

#!/usr/bin/env python
#
## This script converts the .xml(ParaView compatible format) colormaps into Matplotlib or MATLAB format
import sys
import os
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from lxml import etree

## load source xml file
def load_xml(xml):
	try:
		xmldoc = etree.parse(xml)
	except IOError as e:
		print 'The input file is invalid. It must be a colormap xml file. Go to http://sciviscolor.org/home/colormaps/ for some good options'
		print 'Go to http://sciviscolor.org/matlab-matplotlib-pv44/ for an example use of this script.'
		sys.exit()
	data_vals=[]
	color_vals=[]
	for s in xmldoc.getroot().findall('.//Point'):
		data_vals.append(float(s.attrib['x']))
		color_vals.append((float(s.attrib['r']),float(s.attrib['g']),float(s.attrib['b'])))
	return {'color_vals':color_vals, 'data_vals':data_vals}

## source of this function: http://schubert.atmos.colostate.edu/~cslocum/custom_cmap.html#code
def make_cmap(xml):
	vals = load_xml(xml)
	colors = vals['color_vals']
	position = vals['data_vals']
	if len(position) != len(colors):
		sys.exit('position length must be the same as colors')
	elif position[0] != 0 or position[-1] != 1:
		sys.exit('position must start with 0 and end with 1')
	cdict = {'red':[], 'green':[], 'blue':[]}
	for pos, color in zip(position, colors):
		cdict['red'].append((pos, color[0], color[0]))
		cdict['green'].append((pos, color[1], color[1]))
		cdict['blue'].append((pos, color[2], color[2]))
		cmap = mpl.colors.LinearSegmentedColormap('my_colormap',cdict,256)
	return cmap

## Print MATLAB compatible matrix
def print_cmap_matrix(colormap):
	c = 0
	print ''
	print 'matlab-colormap = [',
	while c <= 255:
		if c == 0: pass
		else: print ''
		print str(colormap(c)[0:3]).replace('(','').replace(')',''),
		c = c + 1
	print '];'
	
## This is a quick example plotting the 8 by 1 gradient of the colormap 
## with Matplotlib
def plot_cmap(colormap):
	gradient = np.linspace(0, 1, 256)
	gradient = np.vstack((gradient, gradient))
	fig=plt.figure(figsize=(8,1))
	map=fig.add_subplot(111)
	map.set_frame_on(False)
	map.get_xaxis().set_visible(False)
	map.get_yaxis().set_visible(False)
	fig.tight_layout(pad=0)
	map.imshow(gradient, aspect='auto', cmap=plt.get_cmap(colormap))
	plt.show(fig)

## check for correct file type
def is_xml(string):
	if os.path.isfile(string):
		return string
	else:
		raise argparse.ArgumentTypeError('The file %s does not exist!' % string)
		
## Example usage
if __name__ == '__main__':
	import argparse
	
	parser = argparse.ArgumentParser(description='Convert ParaView compatible colormaps to Matplotlib or MATLAB compatible colormaps.')
	parser.add_argument('-f', '--file-path', dest='path', required=True, type=lambda s: is_xml(s), help='Input file path of .xml colormap with position starting at 0 and ending at 1.')
	parser.add_argument('-m', '--make-matrix', dest='matrix', action='store_true', required=False, help='Print a 3xn matrix of rgb values to copy and paste into MATLAB.')
	
	args = parser.parse_args()

	## construct the colormap
	mycmap = make_cmap(args.path)
	print 'converted successfully!'

	## mycmap is matplotlib compatible object. to query color value out of it:
	print 'example rgba value for data value 0 is: ' + str(mycmap(0.0))

	## MATLAB Users: This fuction will output a RGB matrix to use in MATLAB
	if args.matrix == True:
		print_cmap_matrix(mycmap)

	## Plotting the colormap to test the conversion
	plot_cmap(mycmap)