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

10 views (last 30 days)
My image only has integer values, so including the non-integer values in the colorbar is something I'd like to not do.

Answers (2)

Kevin Holly
Kevin Holly on 15 Sep 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 Comment
Kevin Holly
Kevin Holly on 15 Sep 2022
Edited: Kevin Holly on 15 Sep 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;

Sign in to comment.


Star Strider
Star Strider on 15 Sep 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
.

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!