有没有办法让matplotlib中的gca方法工作?[副本]

yfwxisqw  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(162)

此问题已在此处有答案

gca and latest version of Matplotlib(2个答案)
9天前关闭

配置:

  • Mac OS
  • vs码
  • 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>

我试图得到这个图像:(请参见下图)x1c 0d1x

deikduxw

deikduxw1#

我目前使用的解决方法如下:

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工作。

相关问题