Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - matplotlib - how do I plot a plane from equation?

I have an equation z=0.12861723162963065X + 0.0014024845304814665Y + 1.0964608113924048

I need to plot a 3D plane for this equation in python using matplotlib. I have already tried following this post -- Given general 3D plane equation, how can I plot this in python matplotlib?

However I am unable to set the x,y and z limits for this plane.

Can someone provide me the correct way of converting this equation into 3D plane. Thanks

like image 364
Hussain Niazi Avatar asked Jul 27 '18 13:07

Hussain Niazi


People also ask

How do you define a plane in Python?

Planes are represented by a Plane structure. Planes can be thought of as a zero-based, one-dimensional list containing four elements: the plane's origin (point3D), the plane's X axis direction (vector3d), the plane's Y axis direction (vector3d), and the plane's Z axis direction (vector3d).

How do you plot a 3D function in Python?

We could plot 3D surfaces in Python too, the function to plot the 3D surfaces is plot_surface(X,Y,Z), where X and Y are the output arrays from meshgrid, and Z=f(X,Y) or Z(i,j)=f(X(i,j),Y(i,j)). The most common surface plotting functions are surf and contour. TRY IT!


1 Answers

You have it easy since your equation gives the value of z for any values of x and y.

So choose any limits you like for x and y. You could even use the ones in the web page you linked to. Just calculate the z values according to your equation. Here is code modified slightly from the linked page:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(-1,1,10)
y = np.linspace(-1,1,10)

X,Y = np.meshgrid(x,y)
Z=0.12861723162963065*X + 0.0014024845304814665*Y + 1.0964608113924048

fig = plt.figure()
ax = fig.gca(projection='3d')

surf = ax.plot_surface(X, Y, Z)

And here is the result:

enter image description here

That is not the greatest graph, but now you can modify some of the parameters to get just what you want.

like image 97
Rory Daulton Avatar answered Sep 30 '22 19:09

Rory Daulton