フィルターのクリア

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

17 ビュー (過去 30 日間)
L'O.G.
L'O.G. 2022 年 9 月 15 日
回答済み: Star Strider 2022 年 9 月 15 日
My image only has integer values, so including the non-integer values in the colorbar is something I'd like to not do.

回答 (2 件)

Kevin Holly
Kevin Holly 2022 年 9 月 15 日
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 件のコメント
Kevin Holly
Kevin Holly 2022 年 9 月 15 日
編集済み: Kevin Holly 2022 年 9 月 15 日
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;

サインインしてコメントする。


Star Strider
Star Strider 2022 年 9 月 15 日
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
.

カテゴリ

Help Center および File ExchangeColormaps についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by