surface & colormap with "thermometer" map
Show older comments
I'm using a the surface function to plot a heatmap of a 2D matrix, and that works great. Now I'd like to use a specific colormap: with the clim set to some range [-z, +z] (so that zero is right in the center), I'd like the colormap to go smoothly from blue (-z) to white (zero) to red (+z). This type of colormap is sometimes called "thermometer" or "temperature". Is there an automatic way to get this? So far, the only solution I could come up with is to programmatically define a really long colormap uint8 matrix that goes from [255, 0, 0] to [255, 255, 255] to [0, 0, 255] in increments of 1. But I'm sure that there's a better way.
Accepted Answer
More Answers (1)
Harald
on 15 Oct 2024
Hi,
you could start with the base colors and have MATLAB interpolate in between:
cm = [255, 0, 0; 255, 255, 255; 0, 0, 255];
cmInterp = round(interp1(0:0.5:1, cm, 0:1/(2*255):1));
colormap(cmInterp/255)
Since MATLAB needs RGB values between 0 and 1 anyway, you could do something like this that may not seem that artificial but does not give you exactly the number of colors you have asked for:
cm = [1, 0, 0; 1, 1, 1; 0, 0, 1];
cmInterp = interp1(0:0.5:1, cm, 0:0.001:1);
colormap(cmInterp)
Best wishes,
Harald
Categories
Find more on Color and Styling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!