surface & colormap with "thermometer" map

7 次查看(过去 30 天)
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.

采纳的回答

Voss
Voss 2024-10-15
Seems fine.
N = 255;
cmap = uint8([N*ones(1,N), N:-1:0; 0:N-1, N:-1:0; 0:N, N*ones(1,N)].');
figure
z = peaks(200);
surf(z,'EdgeColor','none')
clim([-4,4])
colormap(cmap)
colorbar
Maybe the 'bwr', 'coolwarm', or 'seismic' colormaps from this FEX submission are "better": https://www.mathworks.com/matlabcentral/fileexchange/120088-200-colormap

更多回答(1 个)

Harald
Harald 2024-10-15
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

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by