1

I'm trying to plot a 3D Decision Boundary, but it does not seem to be working the way it looks, see how it is:

enter image description here

I want it to appear as in this example here:

enter image description here

I do not know how to explain, but in the example above it literally looks like a "wall". And this is what I want to do in my code.

Then follow my code:

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_title('Hello World')
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.set_zlim(-1, 1)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

w = [3,2,1]

x = 1
y = 1
z = 1

x_plan = (- w[1] * y - w[2] * z) / w[0]
y_plan = (- w[0] * x - w[2] * z) / w[1]
z_plan = (- w[0] * x - w[1] * y) / w[2]

ax.plot3D([x_plan, 1, 1], [1, y_plan, 1], [1, 1, z_plan], "lightblue")

plt.show()

P.S.: I'm using:

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

I believe that the problem should be in the calculation, or else in the:

ax.plot3D([x_plan, 1, 1], [1, y_plan, 1], [1, 1, z_plan], "lightblue")

P.S.2: I know that my Boundary Decision is not separating the data correctly, but at the moment this is a detail for me, later I will fix it.

1 Answers1

0

To plot a 3d surface you actually need to use plt3d.plot_surface, see reference.

As an example, this piece of code will generate the following image (Notice the comment on plt3d.plot_surface line):

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

def randrange(n, vmin, vmax):
    '''
    Helper function to make an array of random numbers having shape (n, )
    with each number distributed Uniform(vmin, vmax).
    '''
    return (vmax - vmin)*np.random.rand(n) + vmin

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

n = 10

for c, m, zlow, zhigh in [('r', 'o', 0, 100)]:
    xs = randrange(n, 0, 50)
    ys = randrange(n, 0, 50)
    zs = randrange(n, zlow, zhigh)
    ax.scatter(xs, ys, zs, c=c, marker=m)

for c, m, zlow, zhigh in [('b', '^', 0, 100)]:
    xs = randrange(n, 60, 100)
    ys = randrange(n, 60, 100)
    zs = randrange(n, zlow, zhigh)
    ax.scatter(xs, ys, zs, c=c, marker=m)


ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

xm,ym = np.meshgrid(xs, ys)

ax.plot_surface(xm, ym, xm, color='green', alpha=0.5) # Data values as 2D arrays as stated in reference - The first 3 arguments is what you need to change in order to turn your plane into a boundary decision plane.  

plt.show()

enter image description here

Vinícius Figueiredo
  • 6,300
  • 3
  • 25
  • 44
  • @QuestionsOverflow Are you working with graphing a `a*x+b*y+c*z+d=0` plane, then? Which should be easy to know after you do a correct separation of data. – Vinícius Figueiredo May 11 '17 at 00:59