Search code examples
matlabplotmatlab-figure

Four identical subplots with different view angle


I have made the following 3D plot:

figure
subplot(2,1,1)
hold on
plot3(X_LE, Y_LE,Z, 'red', 'linewidth', 2)
plot3(X_TE, Y_LE,Z, 'red', 'linewidth', 2)
plot3(X_LE, -Y_LE,Z, 'red', 'linewidth', 2)
plot3(X_TE, -Y_LE,Z, 'red', 'linewidth', 2)
plot3([X_LE(end) X_TE(end)],[Y_LE(end) Y_LE(end)],[0 0], 'red', 'linewidth', 2)
plot3([X_LE(end) X_TE(end)],[-Y_LE(end) -Y_LE(end)],[0 0], 'red', 'linewidth', 2)
grid on
axis equal
xlabel('x/b','Interpreter','latex')
ylabel('y/b','Interpreter','latex')
view(-45, 23);

However, I would like to create a 2x2 subplot where in each one of the 4 subplots the view angle will be different.

Instead of copying the whole code 4 times and just changing the view angle, is there some elegant way to do it?

Example of the output im trying to get:

Figure window with 4 subplots, each showing the same data but from different point of view


Solution

  • You could make use of the copyobj function.

    copyobj will allow you to replicate any graphic object you already defined. So the principle is to create your first subplot, then simply copy it 3 times and adjust the position and view of each new copy.

    To use this feature (and for many other reasons), it is good to save the handle of the graphic objects you create. This is usually done by assigning the return value of a graphic instruction to a variable. Ex:

    hp = plot(x,y) ;
    

    Would keep the handle of the plot object in the variable hp, so you can always use this handle to modify the line properties.

    For your specific case it would go like that:

    %% Quick mock up of a 3D triangle (you did not give any sample data)
    x = [0 ;  2 ; 1 ; 0 ] ;
    y = [3 ;  1 ; 5 ; 3 ] ;
    z = [2 ; -1 ; 4 ; 2 ] ;
    
    %% use dummy subplots, just to save their position on a figure
    hf = figure ;
    for k=1:4
        hs = subplot(2,2,k) ;
        axpos{k,1} = hs.OuterPosition ;
    end
    clf(hf) ; % clear all subplots, keep only "axpos" and the empty figure
    
    %% Generate the first subplot
    %% (use your own code for that, but don't forget to retrieve the handles of the figure and the axes)
    figure(hf) ;
    % hs(1) = subplot(2,2,1) ; % use the line below instead. It is equivalent
                               % and it also set the 'hold on' mode for the axe
    hs(1) = axes('parent',hf, 'OuterPosition',axpos{1},'NextPlot','add') ;
    hp = plot3(x,y,z,'red', 'linewidth', 2) ;
    grid on
    axis equal
    xlabel('x/b','Interpreter','latex')
    ylabel('y/b','Interpreter','latex')
    view(-45, 23);
    
    %% Now use "copyobj" to copy the full axes object with the content and labels
    for k=2:4
        hs(k) = copyobj( hs(1) , hf ) ;          % create a copy of the full subplot
        set( hs(k) , 'OuterPosition',axpos{k} )  % reposition it so it doesn't overlap the original
    end
    

    Then all you have to do is to change the view of each subplot according to your needs. This can be done by using the subplot handle as the first argument of the view instruction. For ex:

    %% adjust the view of each subplot
    view( hs(2) ,  25,40)
    view( hs(3) , -25,32)
    view( hs(4) ,  37,92)
    

    Note: If you know the view you want beforehand, you could also place the values in an array at the beginning, and set each axes view directly in the loop where you adjust their position.