Azzera filtri
Azzera filtri

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

17 visualizzazioni (ultimi 30 giorni)
My image only has integer values, so including the non-integer values in the colorbar is something I'd like to not do.

Risposte (2)

Kevin Holly
Kevin Holly il 15 Set 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 Commento
Kevin Holly
Kevin Holly il 15 Set 2022
Modificato: Kevin Holly il 15 Set 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;

Accedi per commentare.


Star Strider
Star Strider il 15 Set 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
.

Categorie

Scopri di più su Colormaps in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by