由于您是Python的初学者,我强烈建议您花点时间查看所使用的包的文档。这个example from matplotlib's documentation回答了你的问题吗?在from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
或者,您也可以查看meshgrid函数的numpy documentation,它给出了一个类似的示例:
^{pr2}$
编辑
如果要手动设置Z的值,可以按照fig = plt.figure()
ax = fig.gca(projection='3d')
x = np.arange(11)
y = np.arange(11)
X, Y = np.meshgrid(x,y)
Z = np.zeros_like(X)
Z[5][5] = 5
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1)
plt.show()