How do you remove non-integer values from a colorbar?

25 visualizaciones (últimos 30 días)
L'O.G.
L'O.G. el 15 de Sept. de 2022
Respondida: Star Strider el 15 de Sept. de 2022
My image only has integer values, so including the non-integer values in the colorbar is something I'd like to not do.

Respuestas (2)

Kevin Holly
Kevin Holly el 15 de Sept. de 2022
Let's say this is an image with a colorbar with non-integer values:
Img = randi(255,25);
imagesc(Img)
h=colorbar;
h.Ticks = [25:55.5:250];
You can change the Ticks with the handle as such:
Img = randi(255,25);
imagesc(Img)
h=colorbar;
h.Ticks = [25:25:250];
  1 comentario
Kevin Holly
Kevin Holly el 15 de Sept. de 2022
Editada: Kevin Holly el 15 de Sept. de 2022
You can change your colormap
Img = randi(4,25)-2;
imagesc(Img)
cmap = [0 0 1; 0 1 0; 1 1 1; 1 0 0];
colormap(cmap)
h=colorbar;
h.Ticks=-1:2;
Knowing I have 3 sections, I will change my colormap to only have 3 colors.
imagesc(Img)
cmap = [0 0 1; 1 1 1; 1 0 0];
colormap(cmap)
h=colorbar;
h.Ticks=-1:2;

Iniciar sesión para comentar.


Star Strider
Star Strider el 15 de Sept. de 2022
This required some coding gymnastics, however it may be what you want —
cm = [1 0 0; 1 1 1; 0 0 1]; % Basic Colormap
cmi = interp1([-1; 0; 2], cm, (-1:2)) % interpolated Colormap
cmi = 4×3
1.0000 0 0 1.0000 1.0000 1.0000 0.5000 0.5000 1.0000 0 0 1.0000
M = randi([-1 2],9) % Matrix
M = 9×9
0 0 1 2 -1 1 -1 -1 -1 1 2 1 0 -1 2 0 -1 0 0 1 2 -1 0 1 2 -1 0 1 1 -1 2 1 -1 -1 1 2 -1 -1 2 0 2 1 1 -1 -1 -1 -1 2 2 2 0 -1 -1 0 -1 1 2 0 -1 2 -1 1 1 2 0 -1 2 0 0 1 1 0 0 0 2 2 1 0 0 1 1
figure
imagesc(M)
colormap(cmi)
hcb = colorbar;
xt = hcb.Ticks;
tix = linspace(min(xt), max(xt), size(cmi,1)*2+1);
hcb.Ticks = tix;
hcb.TickLabels = cell(1,numel(tix));
hcb.TickLabels(2:2:numel(hcb.Ticks)) = compose('%2d',min(xt):max(xt));
hcb.TickLength = 0; % Set TickLength' To 0
.

Categorías

Más información sobre Colormaps en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by