Search code examples
matlab3dplot4d

Matlab: Plot3 not showing the 3rd axis


All the three variables I am using to plot are matrix of size 1x1x100. I am using this code line to plot:

hold on; 
for i=1:100
    plot3(R_L(:,:,i),N_Pc(:,:,i),CO2_molefraction_top_of_window(:,:,i),'o');
    xlabel('R_L');
    ylabel('N_P_c');
    zlabel('CO_2')
end

However, I am not getting the third axis, and hence the third variable CO2_molefraction_top_of_window on the plot. May I know where am I wrong?

Besides the above question, but on the same subject, I want to know if there is any option where I can plot 4 dimensional plot just like the 3 dimensional plot which can be drawn using plot3?


Solution

  • Just a note --- you only need to do the xlabel ylabel zlabel commands once (outside the loop).

    Also:

    • is there any reason your matrices are 1x1x100 instead of just 100x1 or 1x100? Because if you reshape them to 2D you can just do the plotting in one hit.
    • What do you mean by "missing third axis"? When I run your code (or as close as I can get, since you didn't provide a reproducible example), I do get a 3rd axis:

    .

    X = rand(1,1,100); % 1x1x100 X matrix
    Y = rand(1,1,100); % 1x1x100 Y matrix
    Z = rand(1,1,100); % 1x1x100 Z matrix
    % Now, we could do a for loop and plot X(:,:,i), Y(:,:,i), Z(:,:,i),
    % OR we can just convert the matrix to a vector (since it's 1x1x100 anyway)
    %    and do the plotting in one go using 'squeeze' (see 'help squeeze').
    %    squeeze(X) converts it from 1x1x100 (3D matrix) to 100x1 (vector):
    plot3(squeeze(X),squeeze(Y),squeeze(Z),'o')
    xlabel('x')
    ylabel('y')
    zlabel('z')
    

    This gives the following, in which you can clearly see three axes: enter image description here

    If it's the gridlines that you want to make the graph look "more 3D", then try grid on (which is in the examples in the Matlab help file for plot3, try help plot3 from the Matlab prompt):

    grid on
    

    enter image description here

    You will have to clarify "missing third axis" a bit more.