您的示例暴露了 matplotlib 着色算法中的一个错误。阴影
算法计算表面中每个面的法线,uses the color
and its normal vector to shade the facet:
colset = self._shade_colors(color, normals)
虽然理论上平面的法线都是相同的,但实际上,有
由于变幻莫测,数值的变化非常小
浮点运算。这些非常细微的变化被放大了
normalization 因为这个 stretches the minimum and maximum shades 介于 0 和 1 之间。
因此,所有完全平面的表面都容易出现这种着色错误。
当颜色一致时(例如color='b')并且法线都相同(原样
平面的情况),每个面的阴影应该是相同的。归一化应该使阴影为零。所以对于飞机来说,阴影不应该
完全改变颜色。
因此,要解决此错误,请使用 shade=False 关闭着色:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
X = np.arange(1, 4, 0.2)
Y = np.copy(X)
X, Y = np.meshgrid(X, Y)
Z1 = np.copy(X)
Z2 = 2/X
fig = plt.figure()
ax = fig.gca(projection='3d')
surf1 = ax.plot_surface(
X, Y, Z1, rstride=1, cstride=1, color='b',
shade=False, alpha=1.0)
surf2 = ax.plot_surface(
X, Y, Z2, rstride=1, cstride=1, color='r',
shade=True, alpha=1.0)
ax.view_init(elev=-45, azim=-30)
plt.show()
PS。如果您想查看规范化出错的确切位置,请将上面代码中的 shade=False 更改为 shade=True,然后将这些 print 语句放在您安装的 matplotlib/colors.py 中:
resdat = result.data
resdat -= vmin
print(resdat[0, :10])
resdat /= (vmax - vmin)
print(resdat[0, :10])
result = np.ma.array(resdat, mask=result.mask, copy=False)
运行上面的脚本然后打印
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 2.22044605e-16 0.00000000e+00
0.00000000e+00 2.22044605e-16]
[ 0. 0. 0. 0. 0. 0. 1. 0. 0. 1.]
如果resdat 一直保持不变,则resdat 中的所有值在标准化后都将为零。相反,resdat 中的微小错误会被放大为等于 1。这会导致您在蓝色表面上看到有趣的阴影。