您的“集群颜色”是网格的 X、Y、Z 值的一组条件。如果您可以将这些设置为(True/False
值的)掩码,则将颜色映射到它们上很简单。我给出了一个任意条件集的示例,您可以根据自己的需要进行调整。代码改编自http://matplotlib.sourceforge.net/mpl_toolkits/mplot3d/tutorial.html:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
# Some sample data
x_side = np.arange(-5, 5, 0.04)
y_side = np.arange(-5, 5, 0.04)
X, Y = np.meshgrid(x_side,y_side)
# Fake mountains
Z = np.exp(-(X**2+Y**2)) + 2*np.exp(-((X-2)**2+Y**2))
# Assign colors based off some user-defined condition
COLORS = np.empty(X.shape, dtype=str)
COLORS[:,:] = 'b'
COLORS[(Z>.1) * (Z<.3)] = 'r'
COLORS[Z>.3] = 'g'
COLORS[X+Y < -1] = 'k'
# 3D surface plot
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, Z, facecolors=COLORS, rstride=1, cstride=1,
linewidth=0)
plt.show()