Search code examples
matlabplot3dmatlab-figure

Matlab isosurface plots lose 3d when used with subplot


I'm trying to plot a bunch of isosurface plots together with subplot, and I want them to appear 3d and be rotatable. However, I find that when I combine subplot with isosurface the plots appear 2d and are not rotatable. Here's a MRE:

[x,y,z] = meshgrid([-3:0.25:3]); 
V = x.*exp(-x.^2 -y.^2 -z.^2);

for i=1:4
    subplot(2,2,i);

    isosurface(x,y,z,V,1e-4);
end

How can I make this rotatable? If I plot only one isosurface, without subplot, then it's in 3d and rotatable. If I use subplot with surf, I get 4 rotatable 3d surface plots. What am I missing here?


Solution

  • As explained here, subplot first splits your figure into different areas and creates a 2d axis object in one of the areas.

    When you then isosurface after the call to subplot, MATLAB does a 2d projection of isosurface onto the 2d axis.

    surf works differently, because it forces the conversion of the axis object where it is plotted to a 3d axis object. These behaviors can be recreated as follows:

    figure;
    plot(1,1)
    surf([1,2;1,2],[1,1;2,2],[1 1 ; 1 1]);
    

    enter image description here

    figure
    plot(1,1)
    isosurface(x,y,z,V,1e-4);
    

    enter image description here

    A workaround for this is to force the conversion to a 3d Axis object with view(3)(plus re-adding camlight):

    [x,y,z] = meshgrid([-3:0.25:3]); 
    V = x.*exp(-x.^2 -y.^2 -z.^2);
    
    figure;
    
    for ii = 1:4
    
    subplot(2,2,ii);
    isosurface(x,y,z,V,1e-4);
    view(3)
    camlight;
    
    end
    

    enter image description here