How to colorize individual bar in bar3

Hello.
I'm trying to plot 3D graph with bars, in which every bar is colored with color I choose. I found solution for 2D graphs:
Y=[ 1 2 3 ; 4 5 6 ; 3 4 5];
h = bar('v6',Y);
set(h(1),'facecolor','red') % use color name
set(h(2),'facecolor',[0 1 0]) % or use RGB triple
set(h(3),'facecolor','b') % or use a color defined in the help for PLOT
but i can't find out how to use this with bar3 function (use this on 3D). Has anyone any solution?

1 Commento

I ran into this problem when I wanted to mark out "hits" on a 3d representation of a 16x24 plate during HTS. The solution I found works like this: first make a matrix of zeros the same size as the plate. Next use a for loop to change any parts you want marked to have the same value as the inital data but plus .0001 or some small number. Next graph both on the same axis using hold on, and for what was the row of zeros you can use set( name, 'facecolor', 'cyan').
The only downside is that if you have values below zero these columns will appear cyan from the top. This can be solved by also graphing a matrix of values near .0000001 on top of both other matrices.

Accedi per commentare.

 Risposta accettata

Matt Fig
Matt Fig il 15 Apr 2011
Never say never!
Y=[ 1 2 3 ; 4 5 6 ; 3 4 5];
h = bar3(Y);
cm = get(gcf,'colormap'); % Use the current colormap.
cnt = 0;
for jj = 1:length(h)
xd = get(h(jj),'xdata');
yd = get(h(jj),'ydata');
zd = get(h(jj),'zdata');
delete(h(jj))
idx = [0;find(all(isnan(xd),2))];
if jj == 1
S = zeros(length(h)*(length(idx)-1),1);
dv = floor(size(cm,1)/length(S));
end
for ii = 1:length(idx)-1
cnt = cnt + 1;
S(cnt) = surface(xd(idx(ii)+1:idx(ii+1)-1,:),...
yd(idx(ii)+1:idx(ii+1)-1,:),...
zd(idx(ii)+1:idx(ii+1)-1,:),...
'facecolor',cm((cnt-1)*dv+1,:));
end
end
rotate3d
Now S has the handle to each surface so you can change the color of each (or set any other individual property) as you wish. I.e, set(S(1),'facecolor','red'). Also, if you knew ahead of time how many surfaces there would be, you could create a matrix of colors and index into that as S was created....

7 Commenti

I'm speechless...
test
test il 17 Apr 2011
that's want i'm talking about... thx man, now i must get to work:)
Hey! Another question. Can you modify your code in the way, that zero values won't be shown on graph.
Thx in advance.
I am too speechless.Thanks.
Magic. Thanks.
Awesome !
Thank you.

Accedi per commentare.

Più risposte (3)

Does the following not do what you want or have I misunderstood your question?
h = bar3(Y);
set(h(1),'facecolor','red');
set(h(2),'facecolor','blue');
set(h(3),'facecolor','green');
Arnaud

3 Commenti

test
test il 15 Apr 2011
This code colorize entire row of bars, i get 9 bars from which 3 by 3 are the same color. What i want to do is, that i could set color for each bar from graph. I would have to set 9 colors.
You can't, at least as far as I can tell. The handle is a 1x3 vector, so each group of bars is treated as a unit. You double-check this with plottools.
Well done! Great and easy to implement answer!

Accedi per commentare.

Jan
Jan il 15 Apr 2011
Modificato: Jan il 4 Apr 2016
cm = get(gcf,'colormap');
cms = size(cm, 1);
Y = [ 1 2 3 ; 4 5 6 ; 3 4 5];
h = bar3(Y);
for i = 1:length(h)
c = get(h(i), 'CData');
set(h(i), 'CData', ceil(cms * rand(size(c))));
end
[EDITED - same color for all faces of a bar]
Y = [8 9 8; 4 5 6; 3 4 5; 1 2 3];
h = bar3(Y);
[nBar, nGroup] = size(Y);
nColors = size(get(gcf, 'colormap'), 1);
colorInd = randi(nColors, nBar, nGroup);
for i = 1:nGroup
c = get(h(i), 'CData');
color = repelem(repmat(colorInd(:, i), 1, 4), 6, 1);
set(h(i), 'CData', color);
end
This works at least in 2009a and 2015b.
Baha
Baha il 7 Set 2011

0 voti

Can you manipulate your code so that you can partition each column and colorize as you want? For example, Y = [ 1 2 3 ; 4 5 6 ; 3 4 5]; h = bar3(Y); and knowing that two variables are contributing this graph. Furthermore, each column can also be identified by another index. Such as, Y(2,2)=5, and there is an identifier Ei(i=1:3) that E1 contributes Y(2,2) as 1 point, E2 as 2.5 points and E3 1.5 points =total=5. Now, that is what I would like to show on bar graph. i.e. on each column, length(0-1)=red, length(1-3.5)= blue, length(3.5-5)= green. Any idea is appreciated.

Categorie

Richiesto:

il 13 Apr 2011

Commentato:

il 14 Set 2019

Community Treasure Hunt

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

Start Hunting!

Translated by