Filter löschen
Filter löschen

Manual division of the colorbar

9 Ansichten (letzte 30 Tage)
Eylon Vakrat
Eylon Vakrat am 7 Jan. 2023
Beantwortet: Nehemiae am 8 Mär. 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 Kommentare
Rik
Rik am 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 am 8 Jan. 2023
"I don't recognize the interface..."
It is a recent version of the inbuilt COLORMAPEDITOR()

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Nehemiae
Nehemiae am 8 Mär. 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.

Kategorien

Mehr zu Colormaps finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by