Choosing color of bars in bar graph depending on value (not individual, but from colormap)

조회 수: 4 (최근 30일)
Hello
I want to create a 2D- bar graph and Color the bars depending on their values in gray tones. That is what I have so far:
figure()
y = rand(10,1)*3-1.5;
x = linspace(start, end, size(y));
b=bar(x,y,1);
% Defining Colormap
cm=gray(size(y));
colormap(cm);
set(get(b,'children'),'cdata', y);
But this does not work. Neither:
set(get(b,'children'),'CData', y);
I am missing something. Any help?
MJ
  댓글 수: 4
Walter Roberson
Walter Roberson 2018년 6월 28일
Please do not use "end" as a variable name; you will confuse us, you will confuse you, you will confuse MATLAB.
MarionJ
MarionJ 2018년 6월 28일
Sorry. This is actually not my original code. Just edit it for the question. But you are right.

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

채택된 답변

MarionJ
MarionJ 2018년 6월 28일
편집: MarionJ 2018년 6월 28일
Okay. I have found a complicated way of doing it:
figure()
hold on
y= vector;
x = linspace(startpoint, endpoint, size(y,2));
barColorMap= gray(size(y,2));
lscale=linspace(min(y),max(y),size(barColorMap,1));
for cnt=1:size(y,2)
bg=bar(x(cnt),y(cnt),1);
[~,ind]=min(abs(lscale(:)-y(cnt)));
set(bg,'FaceColor',barColorMap(ind,:),'EdgeColor',barColorMap(ind,:));
end

추가 답변 (0개)

카테고리

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

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by