3

What's the difference between a colormap (as required by contour and many other plotting functions) and a simple list of colors? How do I convert a list of colors (where each color is supposed to be represent equal-sized step) into a colormap?

max
  • 49,282
  • 56
  • 208
  • 355

2 Answers2

4

A colormap is a series of colors that represent a notion of quantity. A typical colormap such as jet gives a blue color to low valued markers and red color for high valued markers. So you can use a colormap make another dimension into your picture. For matplotlib if you give a color all marker will have that color (so it's basically an aesthetic feature). If you give a colormap, together with a third variable, that variable will be plotted as color instead for axis space.

In the case of contour each color might represent a higher or lower altitude (for example).

You can set your own discrete colormap using the following recipe:

import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np

def plot_matrix(rm, title='Robot World', cmap=plt.cm.Blues):
    plt.imshow(rm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.tight_layout()
    plt.show()

cmap = colors.ListedColormap(['k','b','y','g','r'])
bounds=[0,1,2,3,4]
norm = colors.BoundaryNorm(bounds, cmap.N)

rm = np.random.randint(0,4,(5,5))
plot_matrix(rm,cmap=cmap)

I brought this recipe from a similar answer I gave in another question you can see here.

Community
  • 1
  • 1
armatita
  • 12,825
  • 8
  • 48
  • 49
  • So what's the difference between a color map and a list of colors? Why did matplotlib bother to define ListedColorMap instead of using a list of colors? – max Apr 08 '16 at 15:17
  • @max Not matplotlib. At the very least, every single major scientific plotting library does this. A colormap is, in effect, a list of colors. The advantage is the behavior you expect to retrieve from this list of colors. When you provide a colormap you are expecting the matplotlib function to automatically recognize the boundaries for each color. When you provide a color (or a list of colors) you are doing that manually. It's all good when it's 2 or 3, but now imagine you want in the dozens. Besides colormaps are well studied to provided different features in images. – armatita Apr 08 '16 at 15:27
  • So conceptually an mpl ColorMap is a map from [0, 1] interval to colors? And because you can define an arbitrary Normalize object, you can achieve any mapping you want with just LinearSegmentedColorMap, or even with the much simpler ListedColorMap, right? – max Apr 08 '16 at 15:39
  • @max That depends on the algorithm you use to build the bins of a colormap (I expect matplotlib to use the same it uses for the bins of a histogram; each bar would be a color). Think of a colormap as a function that returns colors. You input your minimum, your maximum, and a value. The colormap function will return the respective color for the value you gave as argument, given the limits of your data. – armatita Apr 08 '16 at 15:47
0

On the first part of the question: a color map in mpl is intended to represent a function from [0, 1] to colors. mpl only has two very simple classes for that purpose: LinearSegmentedColorMap and ListedColorMap. LinearSegmentedColorMap can represent any color map for which each of the three channels (R, G, B) is a piecewise linear function. ListedColorMap is slightly more restricted in that it requires equal sized pieces in that function (bins). Conceptually, there's no difference between ListedColorMap and lists of colors.

There is no need for any more complicated functions because mpl.Normalize objects can be used to first transform the input data in any desired way into numbers from the [0, 1] interval.

Strictly speaking, it would have been enough to only use ListedColorMap objects created from just two colors, but then Normalize objects would have to do a lot of work that deals with color concepts, which shouldn't be their concern.

max
  • 49,282
  • 56
  • 208
  • 355