Filter löschen
Filter löschen

How to colorize individual bar in bar3

79 Ansichten (letzte 30 Tage)
test
test am 13 Apr. 2011
Kommentiert: Tyson Murray am 14 Sep. 2019
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 Kommentar
bryce Howat
bryce Howat am 1 Aug. 2017
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt Fig
Matt Fig am 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 Kommentare
Samar Singh
Samar Singh am 25 Apr. 2019
Awesome !
Tyson Murray
Tyson Murray am 14 Sep. 2019
Thank you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Arnaud Miege
Arnaud Miege am 13 Apr. 2011
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 Kommentare
Arnaud Miege
Arnaud Miege am 15 Apr. 2011
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.
Ali Rezaei
Ali Rezaei am 22 Mär. 2012
Well done! Great and easy to implement answer!

Melden Sie sich an, um zu kommentieren.


Jan
Jan am 15 Apr. 2011
Bearbeitet: Jan am 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.
  1 Kommentar
Ali Rezaei
Ali Rezaei am 22 Mär. 2012
Well done! Great and easy to implement answer!

Melden Sie sich an, um zu kommentieren.


Baha
Baha am 7 Sep. 2011
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.

Kategorien

Mehr zu Specifying Target for Graphics Output finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by