6

Let's say I have a 3D plane equation:

ax+by+cz=d

How can I plot this in python matplotlib?

I saw some examples using plot_surface, but it accepts x,y,z values as 2D array. I don't understand how can I convert my equation into the parameter inputs to plot_surface or any other functions in matplotlib that can be used for this.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Algorithman
  • 1,309
  • 1
  • 16
  • 39
  • What is your problem exactly? once x and y are defined (using meshgrid for example), z is easily computed... – Julien Jan 19 '18 at 06:33
  • that is the part that I don't understand, can you give an example in the answer? let say I have values a,b in ax and by, how can I put that into meshgrid? – Algorithman Jan 19 '18 at 06:37

1 Answers1

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

a,b,c,d = 1,2,3,4

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

X,Y = np.meshgrid(x,y)
Z = (d - a*X - b*Y) / c

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

surf = ax.plot_surface(X, Y, Z)
Julien
  • 13,986
  • 5
  • 29
  • 53
  • its arguable that `meshgrid` is only a Matlab compatabiltiy wrapper and that numpy's `mgrid` is 'more native' [mgrid](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.mgrid.html) – f5r5e5d Jan 19 '18 at 06:56
  • 1
    what happens if c = 0 – raaj Sep 11 '19 at 20:00
  • @raaj https://stackoverflow.com/questions/53115276/matplotlib-how-to-draw-a-vertical-plane-in-3d-figure – Julien Sep 12 '19 at 00:26
  • @Julien I used the code you provided here to visualize a decision boundary plane for a neural network classification but it did not construct the plane correctly: https://stackoverflow.com/questions/61401114/neural-network-perceptron-visualizing-decision-boundary-as-a-hyperplane-wh – bambi Apr 24 '20 at 17:41
  • @bambi It didn't work because you defined your plane equation incorrectly. – Julien Apr 25 '20 at 09:40