Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 320
Xzhsh Avatar asked Aug 11 '10 19:08

Xzhsh


People also ask

How do I plot a vector in Python using Matplotlib?

Create a matrix of 2×3 dimension. Create an origin point, from where vecors could be originated. Plot a 3D fields of arrows using quiver() method with origin, data, colors and scale=15.

Which function will you use to plot vectors of a field in Python?

In this article, we are going to discuss how to plot a vector field in python. In order to perform this task we are going to use the quiver() method and the streamplot() method in matplotlib module.


2 Answers

For all the copy/pasters out there, here is similar code for Python using matplotlib:

import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D  point  = np.array([1, 2, 3]) normal = np.array([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.dot(normal)  # create x,y xx, yy = np.meshgrid(range(10), range(10))  # calculate corresponding z z = (-normal[0] * xx - normal[1] * yy - d) * 1. /normal[2]  # plot the surface plt3d = plt.figure().gca(projection='3d') plt3d.plot_surface(xx, yy, z) plt.show() 

enter image description here

like image 136
Simon Streicher Avatar answered Sep 30 '22 19:09

Simon Streicher


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') 
like image 28
Jonas Avatar answered Sep 30 '22 18:09

Jonas