Clear Filters
Clear Filters

Manual division of the colorbar

10 views (last 30 days)
Eylon Vakrat
Eylon Vakrat on 7 Jan 2023
Answered: Nehemiae on 8 Mar 2023
Hello everyone,
I'm trying to have a colorbar with only the value 0 will be in a specific grey. all values above, even slightly, will be in a different color, and I want the colorbar to be descrete, meaning 5-10 will have certain color, and not every number will have a different color.
This is the colorbar I have:
And this is the colorbar I want (but prettier ;) ):
Is there any way to do that?
Thank you in advance.
  4 Comments
Rik
Rik on 8 Jan 2023
What exactly did you try? I don't recognize the interface, so I don't know how to most easily extract the colormap from this point.
Stephen23
Stephen23 on 8 Jan 2023
"I don't recognize the interface..."
It is a recent version of the inbuilt COLORMAPEDITOR()

Sign in to comment.

Answers (1)

Nehemiae
Nehemiae on 8 Mar 2023
Hello,
It is possible to create a colormap with colours specified over ranges of inputs, and then input values within the specified ranges are scaled accordingly. This is shown in the case 1 of the code below. Based on the resolution chosen – a small set of values around 0, will be grey. In the second case, based on the kind of plot required (here heatmap was used), it is possible to set the input values with 0 to NaN, and display them in a separate colour using the “MissingDataColor” property.
colors = [255 0 0; 255 140 0; 255 255 0; 0 255 0; 0 0 255; 75 0 130] / 255; % Colors to be used in the colormap
c_levels = [0 5 10 15 20 25 30]; % Levels at which the color changes:
level_res = 0.001; % Resolution of each level
n_colors = round(diff(c_levels) / level_res); % Total number of colors in the map
% Create the colormap
cmap = [];
for i = 1 : numel(n_colors)
cmap = [cmap; repmat(colors(i, :), n_colors(i), 1)];
end
% Case 1: Grey color over a range
cmap1 = cmap;
cmap1(1, :) = [105 105 105] / 255; % Change the color of value 0 to required grey
data = [0; 0.000000001; 0.001; 0.01; 0.1; 1; 5; 10; 15; 20; 25];
figure;
h = heatmap(data);
h.Colormap = cmap1;
caxis([0 30]);
% Case 2: Only zero set to grey in heatmap
cmap2 = cmap;
data(data == 0) = NaN;
figure;
h = heatmap(data);
h.Colormap = cmap2;
h.MissingDataColor = [105 105 105] / 255;
caxis([0 30]);
The documentation on HeatmapChart properties (https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.heatmapchart-properties.html) can be helpful in understanding the above code.

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!