我认为您在3D与2D表面颜色中的错误是由于表面颜色中的数据标准化造成的.如果使用facecolors = plt.cm.BrBG(data / data.max())规范化传递给plot_surface facecolor的数据,结果将更接近您的预期.
如果你只想要一个垂直于坐标轴的切片,而不是使用imshow,你可以使用contourf,从matplotlib 1.1.0开始,3D支持它,
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from matplotlib import cm
# create a 21 x 21 vertex mesh
xx, yy = np.meshgrid(np.linspace(0,1,21), np.linspace(0,1,21))
# create vertices for a rotated mesh (3D rotation matrix)
X = xx
Y = yy
Z = 10*np.ones(X.shape)
# create some dummy data (20 x 20) for the image
data = np.cos(xx) * np.cos(xx) + np.sin(yy) * np.sin(yy)
# create the figure
fig = plt.figure()
# show the reference image
ax1 = fig.add_subplot(121)
ax1.imshow(data, cmap=plt.cm.BrBG, interpolation='nearest', origin='lower', extent=[0,1,0,1])
# show the 3D rotated projection
ax2 = fig.add_subplot(122, projection='3d')
cset = ax2.contourf(X, Y, data, 100, zdir='z', offset=0.5, cmap=cm.BrBG)
ax2.set_zlim((0.,1.))
plt.colorbar(cset)
plt.show()
此代码生成此图像:
虽然这不适用于3D中任意位置的切片,其中imshow solution更好.