0

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:

enter image description here

B:

enter image description here

C:

enter image description here

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

3
  • What have you tried? This looks as easy as bar3(x,y) so I am assuming you have a problem somewhere else. Maybe the values of the data? Show us a MCVE. Commented Apr 24, 2015 at 8:40
  • @AnderBiguri thanks for your comment. What are x and y? I just have a 3 matrices of size 21x2 which I would normally plot them separately using bar. But now I don't know how to plot them separately but still in the same graph if you understand what I mean. Commented Apr 24, 2015 at 8:43
  • stackoverflow.com/help/mcve <- MCVE . Shows us some example! Commented Apr 24, 2015 at 8:46

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for your answer. I've updated my question with examples and screenshots to show what I want to do, so if you could please have a look.
@shepherd So you have 6 data separated in 3 matries? That is confusing! Anyway, to be able to plot all of the 6 then do bar3(horzcat(A,B,C))
But this is not what I want. See in my screenshots how each of A, B and C has 21x 2bars(sticked together)? I want the same thing (not separated) but for the three of them. Do you understand what I mean?
@shepherd Aaahhh! thats Why a MCVE is needed! That explanation was completely absent before. Let me edit my question, if possible.
please let me know when you update your answer. Thank you in advance.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.