Mac
Mac

Reputation: 1031

Creating an specific colourmap in matlab/python

Not sure where is the best place to ask this question but here it goes:

Through a malfunction of my pc screen I got a plot I'd made ages ago, using matlab but works fine in python as well, to go from your regular 'hot' colormap to something completely different as shown in the pic below:

Top part is the original hot and bottom is the 'malfunction' going from dark blue to white-ish from bright pink

Does anyone know how to get that done intentionally?

As it turned out, I like that colour scheme much better, but not sure how/were to get it right.

Upvotes: 0

Views: 2810

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112769

A colormap is just a 3-column matrix where each row defines a color. Specifically, the columns define the R, G, B components respectively. Therefore, you can create colormaps manually.

For example, a colormap what goes from blue to white can be produced by setting the B component to 1 and letting the other two range from 0 to 1:

cmap = [linspace(0,1,256).' linspace(0,1,256).' ones(256,1)];
colormap(cmap)
colorbar('horizontal')

enter image description here

Something closer to your blue/pink/white example can be achieved by rearraging the columns of the hot colormap:

cmap = hot(256);
cmap = cmap(:,[2 3 1]);
colormap(cmap),
colorbar('horizontal')

enter image description here

Many nice colormaps can be produced from the BrewerMap function, available on File Exchange or GitHub.

A problem with manually generated colormaps is that they are not perceptually uniform in general. In contrast, many of Matlab's default colormaps like parula, or Python's like magma, are perceptually uniform. Here's a discussion about uniform and non-uniform colormaps, focused on parula. You can use Python's colormaps in Matlab with this File Exchange function by Ander Biguri.

Here are two examples with Matlab's parula and Python's plasma. In either of them, equal increments along the horizontal axis roughly correspond to a similar perception of "color change".

enter image description here

enter image description here

Upvotes: 4

MrAzzaman
MrAzzaman

Reputation: 4768

It looks like your screen simply inverted the colours. If you want to invert a colormap in MATLAB, you can do this fairly simply by doing the following:

colormap(1-hot);

which will change the reds in the hot map to blues, and the blacks to whites. If you would rather the other way around (i.e., red -> white, black -> blue) you can do this:

colormap(1-flipud(hot));

This should work for any colormap.

Upvotes: 0

Related Questions