MATLAB Answers

10B
0

Colour bars in a plot by data values

Asked by 10B
on 25 Apr 2017
Latest activity Commented on by 10B
on 2 May 2017
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 Comments

Show 1 older comment
Can you put these down in the Answer section, not the comment section?
Done. Sorry about that.

Sign in to comment.

2 Answers

Answer by AstroGuy1984 on 25 Apr 2017
 Accepted Answer

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 Comments

Hello AstroGuy,
Many thanks for the information - I am out of my office now for 36 hours so I wont be able to check any of this, but as soon as I can I will try to implement your code and see how I get on.
Either way - genuine thanks for taking the time to respond and providing such a thorough answer.
Best regards,
10B.
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.

Sign in to comment.


Answer by AstroGuy1984 on 25 Apr 2017

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 Comments

Sign in to comment.