surface & colormap with "thermometer" map

9 vues (au cours des 30 derniers jours)
Hans
Hans le 15 Oct 2024
Réponse apportée : Voss le 15 Oct 2024
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.

Réponse acceptée

Voss
Voss le 15 Oct 2024
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

Plus de réponses (1)

Harald
Harald le 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

Catégories

En savoir plus sur Colormaps dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by