surface & colormap with "thermometer" map
11 ビュー (過去 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.
0 件のコメント
採用された回答
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
0 件のコメント
その他の回答 (1 件)
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Colormaps についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!