I want to plot multiple stacked bar in only one 'detached'
bar plot. E.g, imagine this exactly bar plot, but stacked, instead of one single color.
Asked
Active
Viewed 4,439 times
5

bla
- 25,846
- 10
- 70
- 101

user764186
- 951
- 2
- 9
- 12
-
You mean as in the first image? – Junuxx Oct 31 '12 at 10:49
-
You seem to have found the correct command, at what point do you get stuck exactly? – Dennis Jaheruddin Oct 31 '12 at 10:53
-
The last plot: (Create a 3-D bar graph with the style option 'stacked'): I want to add a z-axis and plot this kind of graph repeatedly in each z(i). Am I clear ? – user764186 Oct 31 '12 at 11:01
-
Not really, at least, not to me... – Junuxx Oct 31 '12 at 13:50
-
2@Junuxx: I think the OP wants to plot multiple stacked bar in only one `'detached'` bar plot. E.g, imagine this exactly [bar plot](http://www.mathworks.com/help/matlab/ref/bar3_detached.png), but stacked, instead of one single color. – Yamaneko Oct 31 '12 at 15:07
-
@Junuxx Exaclty like Victor Hugo said !! – user764186 Oct 31 '12 at 15:11
-
@user764186 Can you update your question to clarify for new visitors? :-) – Yamaneko Oct 31 '12 at 15:13
-
Maybe [this thread](http://www.mathworks.cn/matlabcentral/newsreader/view_thread/258015) can help... – Eitan T Nov 01 '12 at 14:38
1 Answers
3
% Set up two random data sets
data1=rand(10);
data2=rand(10);
% plot the first data set
bh=bar3(data1);
% Loop through each row and shift bars upwards
for i=1:length(bh)
zz = get(bh(i),'Zdata');
k = 1;
% Bars are defined by 6 faces(?), adding values from data2 will
% shift the bars upwards accordingly, I'm sure this could be made
% better!
for j = 0:6:(6*length(bh)-6)
zz(j+1:j+6,:)=zz(j+1:j+6,:)+data2(k,i);
k=k+1;
end
% Reset Zdata in chart
set(bh(i),'Zdata',zz);
end
% Set face colour to red for data1
set(bh,'FaceColor',[1 0 0]);
% Apply hold so that data2 can be plotted
hold on;
% Plot data2
bh=bar3(data2);
% Set face color to blue
set(bh,'FaceColor',[0 0 1]);
hold off;