Colour bars in a plot by data values
조회 수: 8 (최근 30일)
이전 댓글 표시
Hello Community,
I am looking to colour some bar plots individually when a condition is met, ie if they have a value of 4, they will be coluored green, if valued 3 they are magenta and so on.
Following an answer I found by Andrew Schultz here I have put the following together:
% run for loop to colour data individually & when data conditions are met
for i = 1:length(pltvar1srt)
h=bar(i,pltvar1srt(i));
if pltvar1srt(i) == x2634(1,11) && x2634(1,16)
set(h,'FaceColor','b');
hlegend(1) = h;
elseif pltvar1srt(:,2) == 4
set(h,'FaceColor','g');
hlegend(2) = h;
elseif pltvar1srt(:,2) == 3
set(h,'FaceColor','m');
hlegend(3) = h;
elseif pltvar1srt(:,2) == 2
set(h,'FaceColor','r');
hlegend(4) = h;
else pltvar1srt(:,2) == 1
set(h,'FaceColor','k');
hlegend(5) = h;
end
end
but unfortunately what happens is I get a single bar coloured 'b' - so the first condition is met, but the rest of the bars end up coloured 'k', so the last condition is met and overwrites all the other colours.
Could anyone advise on how to fix my loop please?
Many thanks,
10B.
댓글 수: 4
채택된 답변
AstroGuy1984
2017년 4월 25일
In any case, here's one way to do it, assuming your initial condition is as you want it.
for i = 1:length(pltvar1srt)
h=bar(i,pltvar1srt(i));
if pltvar1srt(i) == x2634(1,11) && x2634(1,16)
barColor = 'b';
else
switch pltvar1srt(i)
case 4
barColor = 'g';
case 3
barColor = 'm';
case 2
barColor = 'r';
case 1
barColor = 'k';
otherwise
error('Unhandled case\n');
end
end
set(h, 'FaceColor', barColor);
hlegend(i) = h;
end
Switch is far preferred in cases where it can be used. It is faster and is clearer what is going on. Also note that for switch, you can make several cases have the same outcome by making it a cell. Such as
case {4,5,6}
Would make it so that if the value were 4 5 OR 6, it would do the code in the nest.
추가 답변 (1개)
AstroGuy1984
2017년 4월 25일
Few things:
1) Your final else conditional is not working as you intend it to, but not for the reasons you think. MATLAB is not reading it as else means "if all the other conditions fail". What you want there is another elseif. What I'd suggest is replacing that else with an elseif and then putting an else AFTER that with some sort of error or warning message.
I think what's going on is you're actually FAILING all the other conditionals and because of the mistake with this line, you're missing that.
2) Is that first conditional as you intend? It reads as:
if pltvar1srt(i) == x2634(1,11) && x2634(1,16)
Is x2634(1,16) a logical? Because the way it reads here is as two separate conditions:
a) is pltvar1srt(I) == x2634(1,11)
b) x2634(1,16)
b here will always read as true, unless x2634(1,16) == 0. You can try this in a shell simply by doing something like:
if -1
disp('test')
end
I suspect you meant for something like:
if (pltvar1srt(i) == x2634(1,11)) && (pltvar1srt(i) == x2634(1,16))
Also is x2634 a 2D matrix? Do you mean for it to be x2634(i,11)?
3) I'm confused as to the dimensions of pltvar1srt. Is it a 2D vector or a 1D vector? The conditionals pltvar1srt(:,2) imply it's 2D but that doesn't seem to be what you're describing.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Dialog Boxes에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!