要绘制 3D 曲面,您实际上需要使用plt3d.plot_surface
, 参见参考资料 https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html?highlight=plot_surface#mpl_toolkits.mplot3d.Axes3D.plot_surface.
例如,这段代码将生成以下图像(请注意注释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()