surface & colormap with "thermometer" map

조회 수: 9 (최근 30일)
Hans
Hans 2024년 10월 15일
답변: Voss 2024년 10월 15일
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