possible bug in LinearSegmentedColormap

The attached script creates a colormap containing five colors. At the end of
the script, I print the value of cmap.N, and it is 256 rather than 5.

http://www.nabble.com/file/p25740788/bugdemo.py bugdemo.py

···

--
View this message in context: http://www.nabble.com/possible-bug-in-LinearSegmentedColormap-tp25740788p25740788.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Dr. Phillip M. Feldman wrote:

The attached script creates a colormap containing five colors. At the end of
the script, I print the value of cmap.N, and it is 256 rather than 5.

http://www.nabble.com/file/p25740788/bugdemo.py bugdemo.py

No, it is not a bug. You never told LinearSegmentedColormap that you wanted a map with other than 256 colors, which is the default. If you want a colormap with 3 colors, use a ListedColormap. In fact, as I wrote before, this, together with a BoundaryNorm, is the best way to handle the mapping of discrete colors.

Eric

Hello Eric-

When I look at http://matplotlib.sourceforge.net/api/colors_api.html, the documentation for LinearSegmentedColormap indicates that there is a parameter called "N" but does not specify what this parameter does. Thanks for clarifying this. I will submit a patch to the documentation.

My main problem with ListedColormap is that the documentation for that function does not specify whether the resulting colormap is discrete (piecewise-constant) or continuous (piecewise-linear). I strongly suspect that ListedColormap can only be used to generate discrete colormaps, and I need both types.

Phillip

Eric Firing wrote:

···

Dr. Phillip M. Feldman wrote:

The attached script creates a colormap containing five colors. At the end of
the script, I print the value of cmap.N, and it is 256 rather than 5.

http://www.nabble.com/file/p25740788/bugdemo.py bugdemo.py

No, it is not a bug. You never told LinearSegmentedColormap that you wanted a map with other than 256 colors, which is the default. If you want a colormap with 3 colors, use a ListedColormap. In fact, as I wrote before, this, together with a BoundaryNorm, is the best way to handle the mapping of discrete colors.

Eric

Phillip M. Feldman wrote:

Hello Eric-

When I look at http://matplotlib.sourceforge.net/api/colors_api.html, the documentation for LinearSegmentedColormap indicates that there is a parameter called "N" but does not specify what this parameter does. Thanks for clarifying this. I will submit a patch to the documentation.

My main problem with ListedColormap is that the documentation for that function does not specify whether the resulting colormap is discrete (piecewise-constant) or continuous (piecewise-linear). I strongly suspect that ListedColormap can only be used to generate discrete colormaps, and I need both types.

Yes, ListedColormap is for discrete maps, and LinearSegmentedColormap is for piecewise-linear (to within the resolution provided by the size of the look-up table). There is a new staticmethod of the latter, from_list(), which simplifies the colormap generation for the very common case you are concerned with. It may be that its API needs some tweaking; it probably needs an example in the examples directory (if there isn't one; etc.

I don't agree with your comment about the ListedColormap documentation; I think it is reasonably clear, if you read the whole docstring, and especially the description of N.

Eric

···

Phillip

Eric Firing wrote:

Dr. Phillip M. Feldman wrote:

The attached script creates a colormap containing five colors. At the end of
the script, I print the value of cmap.N, and it is 256 rather than 5.

http://www.nabble.com/file/p25740788/bugdemo.py bugdemo.py

No, it is not a bug. You never told LinearSegmentedColormap that you wanted a map with other than 256 colors, which is the default. If you want a colormap with 3 colors, use a ListedColormap. In fact, as I wrote before, this, together with a BoundaryNorm, is the best way to handle the mapping of discrete colors.

Eric