使用Axes3D时,出现<Figure size 640x480 with 0 Axes>
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 添加第二个特征的平方作为新特征
X_new = np.hstack([X, X[:,1:]**2])
figure = plt.figure()
# 3D可视化
ax = Axes3D(figure)
# 首先画出所有y == 0 的点, 然后再画出所有 y == 1 的点
mask = y == 0
ax.scatter(X_new[mask, 0], X_new[mask, 1], X_new[mask, 2],c = 'b', cmap = mglearn.cm2, s=60)
ax.scatter(X_new[~mask, 0], X_new[~mask, 1], X_new[~mask, 2],c = 'r', marker = '^', cmap = mglearn.cm2, s=60)
ax.set_xlabel("feature0")
ax.set_ylabel("feature1")
ax.set_zlabel("feature1**2")
上面代码,运行之后只出现Text(0.5, 0, 'feature1**2')
和<Figure size 640x480 with 0 Axes>
而没有生成图片
加上%matplotlab inline
和 plt.show()
,也没有变化
gtp是这么说的,然后更换环境那些都没用,没有得到解决方法。
解决办法:将 ax = Axes3D(figure)
改为 ax = figure.add_subplot(projection='3d')
之后成功出现图像