# Colour bars in a plot by data values

1 ビュー (過去 30 日間)
10B 2017 年 4 月 25 日
コメント済み: 10B 2017 年 5 月 2 日
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.
Many thanks,
10B.
##### 4 件のコメント表示非表示 3 件の古いコメント
AstroGuy1984 2017 年 4 月 25 日

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

### 採用された回答

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.
##### 2 件のコメント表示非表示 1 件の古いコメント
10B 2017 年 5 月 2 日
Hello AstroGuy,
Sorry this took longer than I expected to get back to you. I couldn't get your code to work directly - but you did set me on the path to the right answer with the 'switch' command, something i'd not used before, so I have accepted your answer - thanks!
My code ended up like this:
for i = 1:length(pltvar1srt)
h=bar(i,pltvar1srt(i));
if pltvar1srt(i) == x2634(1,11) && x2634(1,16)
set(h,'FaceColor','k');
else
switch pltvar1srt(i,2)
case 4
barColor = 'g';
case 3
barColor = 'm';
case 2
barColor = 'r';
case 1
barColor = 'k';
otherwise
barColor = 'k';
end
set(h, 'FaceColor', barColor);
end
hlegend(i) = h;
end
Which does the job as I wanted following your guidance.
All the best,
10B.

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

### その他の回答 (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.

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

### Community Treasure Hunt

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

Start Hunting!

Translated by