stacked two columns of four in bar3

조회 수: 3 (최근 30일)
Fatemah Alghamedy
Fatemah Alghamedy 2019년 3월 3일
답변: Sreelakshmi S.B 2019년 3월 7일
Hello there,
If I do bar3(data), I got 4 Xs and 4 Ys I want to stacked the the third and fourth column (C and D in Fig)
and keep the first and second columns normal.
I tried this code but did not work
Any one could help me please
data = [ 8.53 31.55 55.43 4.49;
8.79 21.07 40.37 29.77;
9.24 31.20 51.49 8.07;
9.39 37.74 48.01 4.86;];
plotFig = bar3(data(:,1),0.7);
hold on;
plotFig = bar3(data(:,2),0.7);
hold on;
plotFig = bar3(data(:,3:4),0.7,'stacked');
hold on;
% the basic fig
%plotFig = bar3(data,0.7);
%hold on;
namesy =["1", "2", "3", "4"];
set(gca,'ytick',[1:4],'yticklabel',namesy);
namesx =["A", "B", "C" , "D"];
set(gca,'xtick',[1:4],'xticklabel',namesx);

답변 (1개)

Sreelakshmi S.B
Sreelakshmi S.B 2019년 3월 7일
From what i understand,each time bar3 is called, plotting starts from the beginning.So calling bar3 twice-one for the unstacked portions-'A' and 'B' -and the other for the stacked portions-'C' and 'D' -will result in an overlap-you'll get the latest bars and the others will be hidden.
bar3 doesn't seem to have an option to specify the offset of the column while plotting, to overcome this ,or any option to partially stack bars.
one workaround i found was to use the 'stacked_bar3' function.You can get the code and explanation for that here:
This is designed for stacking a 3D matrix but by splitting and combining your data matrix into a 3D matrix as shown below,you can make it work for your 2D matrix.
data1 = [ 8.53 31.55 55.43 ;
8.79 21.07 40.37 ;
9.24 31.20 51.49 ;
9.39 37.74 48.01];
data2 = [ 0 0 4.49;
0 0 29.77;
0 0 8.07;
0 0 4.86;];
temp=zeros(size(data1,1), size(data1,2), 2);
temp(:,:,1)=data1;
temp(:,:,2)=data2;
stacked_bar3(temp);
namesy =["1", "2", "3", "4"];
set(gca,'ytick',[1:4],'yticklabel',namesy);
namesx =["A", "B", "C" , "D"];
set(gca,'xtick',[1:4],'xticklabel',namesx);
The code given above will give you only bars 'C' and 'D' stacked as required (the tops of bars 'A' and 'B' will be coloured unnecessarily but you can probably find a way to avoid this.The origin is also shifted but that shouldn't cause any issues.)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by