How to obtain a cutsomized colorbar at certain color value range?

조회 수: 3 (최근 30일)
MP
MP 2023년 2월 20일
편집: DGM 2023년 2월 20일
Hi,
I would like to obtain a customizd colorbar that has a range from 0-24.
I would like to paint black color for the points falling between 0-6 in colorscale,
paint red for the points falling between 6-18 in colorscale,
again paint black for the points falling between 18-24 in colorscale.
Any suggestions would be greatly appriciated.
scatter(X_axis_val,Y_axis_val,20,Z_axis_val)
MyColBar = [0 0 0; 1 0 0; 0 0 0];
colormap(MyColBar);
caxis([0 24]); colorbar;
The data is as attached below.
Thank you in advance.

채택된 답변

DGM
DGM 2023년 2월 20일
편집: DGM 2023년 2월 20일
You can have a nonuniform discrete colorbar, but it's a bit convoluted:
Drawing from the examples at that link:
% some fake data
x = linspace(0,24,50);
y = x;
z = x;
% intervals/categories in Z (or C)
% the span of this vector defines caxis/clim
zb = [0 6 10/sqrt(2) 18 24];
% interval/category colors
CT0 = lines(numel(zb)-1);
% generate expanded color table & info
nmax = 256; % maximum table length
[CT cidx] = intervalct(zb,CT0,nmax);
cidx1 = cidx(:,2); % trailing block indices
% color data as an RGB color table
% this can be generated however you see fit (imquantize(), etc)
% note my assumption of behavior on bin edges
C = ones(size(z));
nk = numel(zb)-1;
for ck = 1:nk
if ck == 1
C(z<zb(2)) = 1;
elseif ck == nk
C(z>=zb(nk)) = nk;
else
C(z>=zb(ck) & z<zb(ck+1)) = ck;
end
end
C = ind2rgb(C,CT0); % convert to RGB
C = reshape(C,[],3); % reshape into Mx3 color table for use with scatter()
% plot the scatter
hs = scatter(x,y,20,C,'filled');
colormap(CT) % use the expanded colortable
cb = colorbar;
cb.Ticks = zb;
caxis(imrange(zb));
grid on

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 2월 20일
MyColBar = [0 0 0; 1 0 0; 1 0 0; 0 0 0];
You cannot have color sections of different widths, but you can duplicate colors.
  댓글 수: 1
MP
MP 2023년 2월 20일
편집: MP 2023년 2월 20일
Ah! Thanks for the clarification! Could you please suggest how to know the default color section width on a colorbar? In my case I have 0-24 basically 24-sections. So May I assume that I will have to copy the color code 24-times?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Purple에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by