Matlab:3D 堆积条形图 [英] Matlab: 3D stacked bar chart
问题描述
我正在尝试创建一个 3D 堆叠条形图,如以下问题所示:
如您所见,中央条应加起来为 380,但只有 280 高.其中一个数据集的条形似乎呈现在其他两个数据集的内部",这也可以解释为什么这些条形具有 3 条中央分隔线,而不是我们预期的 2 条.
我使用的代码是:
core=bar3(test1);设置(核心,'FaceColor',[1 0 0]);%红色的对于 i=1:长度(核心)zz=get(core(i),'Zdata');k=1;对于 j= 0:6:(6*length(core)-6)zz(j+1:j+6,:)=zz(j+1:j+6,:)+test2(k,i);k=k+1;结尾set(core(i),'Zdata',zz);结尾坚持,稍等核心=bar3(test2);set(core,'FaceColor',[0 1 1]);%青色推迟对于 i=1:长度(核心)zz=get(core(i),'Zdata');k=1;对于 j= 0:6:(6*length(core)-6)zz(j+1:j+6,:)=zz(j+1:j+6,:)+test3(k,i);k=k+1;结尾set(core(i),'Zdata',zz);结尾坚持,稍等核心=bar3(test3);set(core,'FaceColor',[1 1 0]);%yellow推迟
如何让横条正确上移?或者,如何使用 Matlab 代码为数据制作 3D 堆积条形图?非常感谢您的帮助,感谢您的时间.
在第一个循环中使用:
zz(j+1:j+6,:)=zz(j+1:j+6,:)+test2(k,i)+test3(k,i);
代替zz(j+1:j+6,:)=zz(j+1:j+6,:)+test2(k,i);
.
或者您可以在第二个循环中使用相同的字符串.
I'm trying to create a 3D stacked bar chart as in this question: 3D stacked bars in Matlab. However unlike that question I want to use 3 datasets instead of 2. I think I have a (probably simple) problem where the bars don't shift up correctly or something similar.
The three datasets I have are called test1, test2 and test3 and are respectively:
5 10 7
1 100 0
1 3 2
10 15 10
10 80 10
5 5 15
10 10 10
20 200 20
30 10 30
And plotting them produces this:
As you can see the central bar should add up to 380 but is only 280 tall. The bars for one of the datasets seem to be rendered "inside" the other two datasets, which would also explain why the bars have 3 central separation lines instead of the 2 we'd expect.
The code I'm using is:
core=bar3(test1);
set(core,'FaceColor',[1 0 0]); %red
for i=1:length(core)
zz=get(core(i),'Zdata');
k=1;
for j= 0:6:(6*length(core)-6)
zz(j+1:j+6,:)=zz(j+1:j+6,:)+test2(k,i);
k=k+1;
end
set(core(i),'Zdata',zz);
end
hold on
core=bar3(test2);
set(core,'FaceColor',[0 1 1]);%cyan
hold off
for i=1:length(core)
zz=get(core(i),'Zdata');
k=1;
for j= 0:6:(6*length(core)-6)
zz(j+1:j+6,:)=zz(j+1:j+6,:)+test3(k,i);
k=k+1;
end
set(core(i),'Zdata',zz);
end
hold on
core=bar3(test3);
set(core,'FaceColor',[1 1 0]);%yellow
hold off
How do I make the bars shift up properly? Or alternatively, how can I use Matlab code to make a 3D stacked bar chart for the data? Any help greatly appreciated, thanks for your time.
In the first loops use:
zz(j+1:j+6,:)=zz(j+1:j+6,:)+test2(k,i)+test3(k,i);
instead of zz(j+1:j+6,:)=zz(j+1:j+6,:)+test2(k,i);
.
Or you can use the same string in the second loops.
这篇关于Matlab:3D 堆积条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!