File Exchange

image thumbnail

PyColormap4Matlab

version 1.3.1 (276 KB) by Konrad
Simple Matlab and python script that import colormaps from matplotlib into Matlab.

40 Downloads

Updated 09 Jul 2020

GitHub view license on GitHub

https://github.com/f-k-s/PyColormap4Matlab/blob/master/README.md
Simple Matlab and python script that import colormaps from matplotlib into Matlab.

getPyPlot_cMap returns any colormap implemented in the matplotlib python library. It calls a python script that writes the colormap matrix into a temporary file, i.e. PYTHON (and the matplotlib module) is REQUIRED. However, the advantage is that you get all the colormaps implemented in matplotlib and that you can specify the number of RGB quantization levels, i.e. the number of colors of the colormap.

A list of colormap names is provided in the function help section. getPyPlot_cMap('!GetNames') returns a cellstring containing all available colormap names. See https://matplotlib.org/examples/color/colormaps_reference.html for an illustration of colormaps.

=================================================
Usage

cMapNames = getPyPlot_cMap('!GetNames')
Returns a cellstring containing all available colormap names.

cMap = getPyPlot_cMap(cMapName)
Returns the colormap cMapName with the default of 128 colors. cMap will be a 128x3 matrix.

cMap = getPyPlot_cMap(cMapName, NumberOfColors)
Specify the number of colors, i.e. the number of rows in cMap.

cMap = getPyPlot_cMap(cMapName, NumberOfColors, keepAlphaChannel)
If keepAlphaChannel is not 0 cMap has a 4th column containing the alpha channel.

cMap = getPyPlot_cMap(cMapName, NumberOfColors, keepAlphaChannel, pythonSystemCommand)
Lets you specify the python command (possibly including a path, see below) used to execute the python script.

=================================================
Errors

If you have python installed but Matlab says 'There was an error executing the command... System returned:...' you can try to pass the path to your python installation explicitly as the 4th parameter, e.g.:

cmp = getPyPlot_cMap('Accent', [], [], '"c:\Program Files\Python37\python.exe"');

Note the double-quotes around the path, which are neccessary because of the containing space character.

Cite As

Konrad (2020). PyColormap4Matlab (https://github.com/f-k-s/PyColormap4Matlab), GitHub. Retrieved .

Comments and Ratings (7)

Thanks a lot for this contribution. Very simple and valuable!

Konrad

Thank you Stephen, I’m flattered :)
I totally agree that my implementation is rather slow; it’s not intended to be used in a larger application or whenever performance matters.
And thank you for your comments on the CubeHelix implementation in matplotlib; I wasn’t aware of this.

Stephen Cobeldick

Sorry: PYPLOT.CM should be PYPLOT.PY.

Stephen Cobeldick

Neat concept, I really like the idea of this. Like all good ideas, it is simple at heart but does something fundamentally useful.

Using a temporary data file is innovative, but repeating those system commands and file I/O on every function call is unlikely to be efficient: perhaps PERSISTENT or MEMOIZE could be used here to speed up subsequent calls.

As an aside, note that CubeHelix is not just an array of RGB values but is actually an algorithm which can generate a wide variety of colormaps, for which it has several parameters which the user uses to specify a colormap, independently of the colormap size.

An implementation of the CubeHelix algorithm is actually contained in MatPlotLib's _CM.PY module, but only its default parameters are used when it is imported by CM.PY (which in turn is imported by PYPLOT.CM). This is a shame, because the default values "aren't very good" (according to http://inversed.ru/Blog_2.htm#Cubehelix), which means that users of MatPlotLib (and this FEX submission) are likely put-off by the default CubeHelix colormap, and sadly never have the opportunity to actually experiment with the CubeHelix algorithm itself.

A related discussion on this topic: https://github.com/matplotlib/matplotlib/issues/9895/

A shameless plug of my own FEX: www.mathworks.com/matlabcentral/fileexchange/43700-cubehelix-colormap-generator

Bridge Jay

Konrad

Thanks Piercarlo for letting me know!
I added the solution for this issue to the description above.
If the problem occurs to anyone else: pass the path to your python installtion as the 4th argument.

Piercarlo Giacomel

An error is occurring to me:

"Error using getPyPlot_cMap (line 93)
There was an error executing the command python ...(path)\pyplotCMap2txt.py
System returned:
"python" is not recognized as an internal or external command, or an exe or file batch.
Impossible to find the specified path."

What should I do to fix it?

Updates

1.3.1

Added Live Script

1.3.0

Fixed issue when path to python script contain white spaces or special characters.
Thanks to Piercarlo!

Updated Description.

1.2.0

List of colormap names (when retrieved with !getNames) is now generated by python script instead of hard-coded in Matlab script.

1.1.0

getNames implemented

1.0.2

Description updated

1.0.1

Image added (taken from matplotlib.org)

MATLAB Release Compatibility
Created with R2015a
Compatible with any release
Platform Compatibility
Windows macOS Linux