matlabmatplotlibplotscipy

Plot a plane based on a normal vector and a point in Matlab or matplotlib


How would one go plotting a plane in matlab or matplotlib from a normal vector and a point?


Solution

  • For Matlab:

    point = [1,2,3];
    normal = [1,1,2];
    
    %# a plane is a*x+b*y+c*z+d=0
    %# [a,b,c] is the normal. Thus, we have to calculate
    %# d and we're set
    d = -point*normal'; %'# dot product for less typing
    
    %# create x,y
    [xx,yy]=ndgrid(1:10,1:10);
    
    %# calculate corresponding z
    z = (-normal(1)*xx - normal(2)*yy - d)/normal(3);
    
    %# plot the surface
    figure
    surf(xx,yy,z)
    

    enter image description here

    Note: this solution only works as long as normal(3) is not 0. If the plane is parallel to the z-axis, you can rotate the dimensions to keep the same approach:

    z = (-normal(3)*xx - normal(1)*yy - d)/normal(2); %% assuming normal(3)==0 and normal(2)~=0
    
    %% plot the surface
    figure
    surf(xx,yy,z)
    
    %% label the axis to avoid confusion
    xlabel('z')
    ylabel('x')
    zlabel('y')