Colour bars in a plot by data values
9 ビュー (過去 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 件のコメント
Image Analyst
2017 年 4 月 25 日
Can you put these down in the Answer section, not the comment section?
採用された回答
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!