配置:
- 苹果操作系统
- 对代码
- python-3.x
错误:
代码:
fig = plt.figure(figsize=(10, 6))
ax = fig.gca(projection='3d')
surf1 = ax.plot_surface(X, Y, Z, rstride=2, cstride=2, cmap=mpl.cm.coolwarm, linewidth=0.5, antialiased=True) # Plots the original function surface
surf2 = ax.plot_wireframe(X, Y, Z, rstride=2, cstride=2, label='regression') # Plots the regression surface
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('f(x, y)')
ax.legend()
fig.colorbar(surf, shrink=0.5, aspect=5)
报错信息:
TypeError Traceback (most recent call last)
Cell In\[61\], line 2
1 fig = plt.figure(figsize=(10, 6))
2 ax = fig.gca(projection='3d')
3 surf1 = ax.plot_surface(X, Y, Z, rstride=2, cstride=2, cmap=mpl.cm.coolwarm, linewidth=0.5, antialiased=True) # Plots the original function surface
4 surf2 = ax.plot_wireframe(X, Y, Z, rstride=2, cstride=2, label='regression') # Plots the regression surface
TypeError: FigureBase.gca() got an unexpected keyword argument 'projection'
<Figure size 1000x600 with 0 Axes>
我目前使用的解决方法如下:
ax = fig.add_subplot(projection='3d')
给出以下代码:
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(projection='3d')
surf1 = ax.plot_surface(X, Y, Z, rstride=2, cstride=2, cmap=mpl.cm.coolwarm, linewidth=0.5, antialiased=True) # Plots the original function surface
surf2 = ax.plot_wireframe(X, Y, Z, rstride=2, cstride=2, label='regression') # Plots the regression surface
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('f(x, y)')
ax.legend()
fig.colorbar(surf, shrink=0.5, aspect=5)
但我更愿意找到使 gca 工作的解决方案。