简体   繁体   中英

Use bar3 instead of bar to plot multiple data in Matlab

I have 3 matrices A , B and C where each of them is of size 21x2 . I use bar to plot each one separately. I'm wondering how I can plot the three together using bar3 ?

So using this code:

A=rand(21,2);
B=rand(21,2);
C=rand(21,2);
fig=figure();b1=bar(A);
fig2=figure();b2=bar(B);
fig3=figure();b3=bar(C);

will generate these three figures:

A:

在此处输入图片说明

B:

在此处输入图片说明

C:

在此处输入图片说明

And what I want to do is that I want them to be all the the same figure but plotted behind each other in the z direction to be something like this

The idea is to create new variables that contain all the data you want in each row intercalated with NaNs. Just that change gives you almost the solution.

for ii=1:size(A,1)
   A1((ii-1)*3+1)=A(ii,1);
   A1((ii-1)*3+2)=A(ii,2);
   A1((ii-1)*3+3)=NaN;

   B1((ii-1)*3+1)=B(ii,1);
   B1((ii-1)*3+2)=B(ii,2);
   B1((ii-1)*3+3)=NaN;

   C1((ii-1)*3+1)=C(ii,1);
   C1((ii-1)*3+2)=C(ii,2);
   C1((ii-1)*3+3)=NaN;
end

h=bar3(horzcat(A1',B1',C1'))

However, I am guessing that you also want to modify the colors. To do this, the idea is that you can get the colour data for each bar row using get(h(nrow),'Cdata') .

with this trick and your own colormap you should be able to colour the bars independently. It is not straightforward, but where's the fun if its easy!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM