Skip to content Skip to sidebar Skip to footer

Plot A 3d Boundary Decision In Python

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: I want it to appear as in this example here: I do not know how to e

Solution 1:

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

defrandrange(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 = 10for 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

Post a Comment for "Plot A 3d Boundary Decision In Python"