Matplotlib 如何将曲面绘制到每个子地块中

Matplotlib 如何将曲面绘制到每个子地块中,matplotlib,subplot,surface,Matplotlib,Subplot,Surface,我想把两个曲面划分成两个子地块。到目前为止,曲面绘制得很好,但是所有曲面都绘制到第一个子地块图中 代码如下所示: import numpy as np import matplotlib.pyplot as plt from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter from mpl_toolkits.mplot3d import Axes3D #... fig

我想把两个曲面划分成两个子地块。到目前为止,曲面绘制得很好,但是所有曲面都绘制到第一个子地块图中

代码如下所示:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from mpl_toolkits.mplot3d import Axes3D 

#...

fig = plt.figure()
ax = fig.add_subplot(121, projection='3d')

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.winter,
                       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'))
ax.set_xlabel(r'X')
ax.set_ylabel(r'Y')
ax.set_zlabel(r"Z-axis = weight $w(X,Y)$")

fig.colorbar(surf, shrink=0.5, aspect=5)

ax2 = fig.add_subplot(122, projection='3d')
surf2 = ax.plot_surface(X, Y, W, rstride=1, cstride=1, cmap=cm.winter,
                       linewidth=0, antialiased=False)
ax2.zaxis.set_major_locator(LinearLocator(10))
ax2.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
ax2.set_xlabel(r'X')
ax2.set_ylabel(r'Y')
ax2.set_zlabel(r"Z-axis = approximated weight $w(X,Y)$")

fig.colorbar(surf2, shrink=0.5, aspect=5)

plt.show()
在结果中,右侧子地块图为空,而左侧子地块图包含两个曲面。我想把每个曲面画成一个单独的图形区域


您需要在
ax2
上绘制
surf2
(您当前在
ax
上绘制它)


哦,伙计,谢谢。我完全忽略了这一点。典型的复制和粘贴错误:)
surf2 = ax2.plot_surface(X, Y, W, rstride=1, cstride=1, cmap=cm.winter,
                         linewidth=0, antialiased=False)