See https://www.mathworks.com/matlabcentral/fileexchange/69470-custom-colormap The function expects positions in the range 0 to 1, so you will need to rescale() your valid key values from whatever fixed range they have now to the [0 1] range.It looks like you might have the range -1 to +1 so add 1 to the key values and divide the result by 2 to get the [0 1] position for the purposes of the function. Then when you colormap() the new map into place, be sure the caxis([-1 1]) or as appropriate the fixed values. Each "slot" in a colormap is exactly the same size: for a colormap with N rows, each entry in the colormap covers 1/N of the defined range of values. It is not possible to say that 0.2 to 0.4 is a single slot that is one color but that other slots are to be different widths. If you want different colors in the map to represent different data spans, then what you have to do is use more rows in the colormap but repeat individual colors. For example if you had one color of desired width 0.2 and another of desired width 0.18 then you could use enough rows that an individual slot covered 0.02 of the range, and the 0.2 repeated a color 10 times and the 0.18 repeated it color 9 times. You cannot just create a colormap with arbitrary breakspoints, colormap processing is always a linear mapping process. Answer from Walter Roberson on mathworks.com
🌐
MathWorks
mathworks.com › matlab › graphics › labels and styling › color and styling
Control Colormap Limits - MATLAB & Simulink
For many types of visualizations you create, MATLAB® maps the full range of your data to the colormap by default. The smallest value in your data maps to the first row in the colormap, and the largest value maps to the last row in the colormap.
🌐
MathWorks
mathworks.com › matlab › graphics › labels and styling › color and styling
colormap - View and set current colormap - MATLAB
An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities can be double or single values in the range [0, 1], or they can be uint8 values in the range [0, 255]. For example, this matrix defines a ...
🌐
MathWorks
mathworks.com › matlab › graphics › labels and styling › color and styling
clim - Set colormap limits (Renamed from caxis in R2022a) - MATLAB
Set the colormap limits for multiple axes or charts at once by specifying the target argument as an array of axes or standalone visualizations. To align the function name with the CLim axes property, caxis is now called clim. ... Run the command by entering it in the MATLAB Command Window.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 1891415-change-the-range-of-your-sections-in-your-colormap
change the range of your sections in your colormap - MATLAB Answers - MATLAB Central
January 10, 2023 - Hello, I want my section to be red [-1 -0.1], green [-0.1 0.1], blue [0.1 1]. now it just divide it in 3 equal sections. However, if I try to implement limits, it gives the following error: 'must ...
Top answer
1 of 1
4

There are quite a few different colormaps, and you can make your own custom colormap too. Use doc colormap to see them in more detail:

If you are using the HSV colormap and only seeing blue to red colours then it is because your colour axis limits are such that only that part of the colormap is being sampled. So if your data spans from 0.5 to 1.0 but you set the colour axis to caxis([0.0 1.0]) then you would only see half of the colours in the colormap.

In order to get a colormap like you describe, you can use this approach where you specify the minimum and maximum colours and create a colormap that blends from one to the other. Note that you will have to set your colour axis values according to the limits of your plot appropriately (commands like surf automatically stretch to include all the colours).

% number of map indices
Nmap = 64;

% colormap from cyan to red
cMin1 = [0 1 1];
cMax1 = [1 0 0];
cMap1 = zeros(Nmap,3);
for i = 1:Nmap;
    cMap1(i,:) = cMin1*(Nmap - i)/(Nmap - 1) + cMax1*(i - 1)/(Nmap - 1);
end

% colormap from white to red
cMin2 = [1 1 1];
cMax2 = [1 0 0];
cMap2 = zeros(Nmap,3);
for i = 1:Nmap;
    cMap2(i,:) = cMin2*(Nmap - i)/(Nmap - 1) + cMax2*(i - 1)/(Nmap - 1);
end

% make up some data
Z = linspace(0,1,100)'*ones(1,100);

% plot with HSV colormap
figure
surf(Z,'edgealpha',0);
colormap('hsv');

% plot with cyan-to-red colormap
figure
surf(Z,'edgealpha',0);
colormap(cMap1);

% plot with white-to-red colormap
figure
surf(Z,'edgealpha',0);
colormap(cMap2);

Which should produce:

Find elsewhere
🌐
MathWorks
mathworks.com › matlabcentral › answers › 422753-how-to-set-a-value-range-for-predefined-colormap
How to set a value range for predefined colormap? - MATLAB Answers - MATLAB Central
October 8, 2018 - I would like to set like 0 is the lowest value (in 'winter' blue) and 20 (in 'winter' yellow) the highest. If one of the plots show maximum values of 5 there shouldn't be any yellow color.
🌐
MathWorks
mathworks.com › matlab › graphics › labels and styling › color and styling
Change Color Scheme Using a Colormap - MATLAB & Simulink
You can also create your own colormap as an m-by-3 array. Each row in the array contains the red, green, and blue intensities of a different color. The intensities are in the range [0,1]. Here is a simple colormap that contains three entries.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 289537-how-define-colormap-for-range-of-value-of-z-data
how define colormap for range of value of z data - MATLAB Answers - MATLAB Central
June 13, 2016 - i want create colormap for z value by following range : 100-1200 blue 1200-1300 red 1300-1400 yellow 1400 to up green, please help me to create this colormap. thanks. Sign in to comment.
🌐
MathWorks
mathworks.com › matlab › graphics › labels and styling › color and styling
How Image Data Relates to a Colormap - MATLAB & Simulink
When you display images using the image function, you can control how the range of pixel values maps to the range of the colormap. For example, here is a 5-by-5 magic square displayed as an image using the default colormap. ... A contains values between 1 and 25. MATLAB® treats those values ...
🌐
MathWorks
mathworks.com › matlabcentral › answers › 248210-how-to-set-colorbar-limits
How to set colorbar limits? - MATLAB Answers - MATLAB Central
October 12, 2015 - If you want to define the colormap to have fewer colours that the default (or a certain specific set of colours), you just have to tell it. See the documentation for colormap for details. The colormap itself is a (Nx3) matrix. (Please do not use min and max as variable names. They are built-in MATLAB functions, and will cause problems if you want to use the functions later in your code.)
🌐
MathWorks
mathworks.com › matlabcentral › answers › 542924-how-to-scale-the-range-of-a-custom-colormap-correctly
How to scale the range of a custom colormap correctly? - MATLAB Answers - MATLAB Central
June 5, 2020 - I have successfully scaled the colormap limits of the axes to the one that I wanted. Glad and thankyou for the answer. ... https://www.mathworks.com/matlabcentral/answers/542924-how-to-scale-the-range-of-a-custom-colormap-correctly#comment_886595
🌐
Educative
educative.io › answers › how-to-create-a-custom-colormap-in-matlab
How to create a custom colormap in MATLAB
Follow the steps below to create a custom colormap: ... Each row of the matrix represents a color. Each column represents the intensity of red, green, and blue. Values should range from 0 to 1.
🌐
MathWorks
mathworks.com › matlab › graphics › images
imagesc - Display image with scaled colors - MATLAB
Create matrix C. Display an image ... limits so that image uses the full range of the colormap, where the smallest value in C maps to the first color in the colormap and the largest value maps to the last color....