如何将曲面绘制到每个子图中

时间:2015-07-06 13:34:45

标签: 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 = 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()

在结果中右侧子图表为空,而左侧子图表包含两个表面。我想将每个表面绘制成一个单独的图形区域。

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要在surf2上绘制ax2(您目前正在ax上绘制)

surf2 = ax2.plot_surface(X, Y, W, rstride=1, cstride=1, cmap=cm.winter,
                         linewidth=0, antialiased=False)