Manual division of the colorbar

13 次查看(过去 30 天)
Eylon Vakrat
Eylon Vakrat 2023-1-7
回答: Nehemiae 2023-3-8
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 个评论
Rik
Rik 2023-1-8
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 2023-1-8
"I don't recognize the interface..."
It is a recent version of the inbuilt COLORMAPEDITOR()

请先登录,再进行评论。

回答(1 个)

Nehemiae
Nehemiae 2023-3-8
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.

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by