Matplotlib3。4gca()方法已经弃用,替代方法
今天在应用最小二乘回归方法对多维进行科学试验时候,出现一个警告C:Users imAppDataLocalTempipykernel_263802953945051.py:23: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot(). ax = fig.gca(projection ="3d")
import numpy as np
def fm(p):
x,y = p
return np.cos(x) + 0.7*x +np.sin(y) + 0.05*y**2
x = np.linspace(0,100,50)
y = np.linspace(0.100,50)
X,Y = np.meshgrid(x,y)
Z = fm((X,Y))
x =X.flatten()
y= Y.flatten()
#print(x)
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(10,10))
ax = fig.gca(projection ="3d")
surf = ax.plot_surface(X,Y,Z,rstride=2,cstride=2,cmap="coolwarm",linewidth=0.3,antialiased=True)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("f(x,y)")
fig.colorbar(surf,shrink=0.5,aspect=0.5)
后查资料得出解决方法把
ax = fig.gca(projection ="3d")
替换为
ax = fig.add_subplot(projection="3d")
然后再在jupyter notebook里运行就不报错了。