Search code examples
imagematlabplotmatlab-figureaxes

Display an image on the X-Z plane (instead of the default X-Y)


I can get an image plot on the X-Y plane using imagesc, but now I would like to have it on the X-Z plane for further usage. Is there any way to do so? Thanks!


Solution

  • I'd use surface instead of imagesc:

    INPUT = [3,4,5
             4,5,6];
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    figure(); 
    ZZ = padarray(INPUT,[1 1],0,'post'); % See note #2
    [XX,YY] = meshgrid((1:size(INPUT,2)+1)-0.5,(1:size(INPUT,1)+1)-0.5);
    % imagesc
    subplot(3,1,1); imagesc(INPUT); xlim([0 4]); ylim([0.5 2.5]);
    view([-50 50]); xlabel('x'); ylabel('y'); zlabel('z'); grid on;
    title('imagesc');
    % Normal (X-Y):
    subplot(3,1,2); surface(XX,YY,0*XX,ZZ,'EdgeColor','none','FaceColor','flat'); 
    view([-50 50]); xlabel('x'); ylabel('y'); zlabel('z'); axis ij; box on; grid on;
    title('X-Y surface'); caxis([min(INPUT(:)),max(INPUT(:))]);
    % Rotated (X-Z):
    subplot(3,1,3); surface(XX,0*ZZ,YY,ZZ,'EdgeColor','none','FaceColor','flat');
    view([-50 50]); xlabel('x'); ylabel('y'); zlabel('z'); axis ij; box on; grid on;
    title('X-Z surface'); caxis([min(INPUT(:)),max(INPUT(:))]);
    

    Several notes:

    1. You might need to flipud or fliplr some of the inputs in the 2nd surface plot (depending on how you define the Y -> Z transition).
    2. Presentation-wise the output is the same, but, if you try to compare the values of the nodes you will not get the same result between the X-Y and the imagesc outputs. The reason for this is that the surface is defined using the vertices, and an Image object is defined using the value in the center of each square.

    Output:

    Comparison of outputs