<p><a href="https://stackoverflow.com/a/6543777/3926689">This</a>答案解决4d曲面绘图问题。它使用matplotlib的<code>plot_surface</code>函数,而不是<code>plot_trisurf</code>。</p>
<p>基本上,您需要将x、y和z变量重塑为具有相同维度的二维数组。若要将第四个维度添加为颜色映射,必须提供与轴变量具有相同维度的另一个二维数组。</p>
<p>下面是与x值相对应的颜色映射的3d绘图的示例代码。参数<code>facecolors</code>用于根据您的喜好更改颜色映射。注意,它的值是从<code>matplotlib.cm.ScalarMappable</code>类中的<code>to_rgba()</code>函数获取的。</p>
<pre><code>import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# domains
x = np.logspace(-1.,np.log10(5),50) # [0.1, 5]
y = np.linspace(6,9,50) # [6, 9]
z = np.linspace(-1,1,50) # [-1, 1]
# convert to 2d matrices
Z = np.outer(z.T, z) # 50x50
X, Y = np.meshgrid(x, y) # 50x50
# fourth dimention - colormap
# create colormap according to x-value (can use any 50x50 array)
color_dimension = X # change to desired fourth dimension
minn, maxx = color_dimension.min(), color_dimension.max()
norm = matplotlib.colors.Normalize(minn, maxx)
m = plt.cm.ScalarMappable(norm=norm, cmap='jet')
m.set_array([])
fcolors = m.to_rgba(color_dimension)
# plot
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X,Y,Z, rstride=1, cstride=1, facecolors=fcolors, vmin=minn, vmax=maxx, shade=False)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
fig.canvas.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/w9wHB.png" rel="noreferrer"><img src="https://i.stack.imgur.com/w9wHB.png" alt=""/></a></p>
<p>我引用的答案(和其他)提到,您应该规范化您的第四维度数据。这似乎可以通过显式设置colormap的限制来避免,就像我在代码示例中所做的那样。</p>