我有一个带有标签的数据集,我想用根据它们的标签着色的点进行绘制。有没有一种简单的方法来获取绘图中的当前行号,以便我可以确定该点属于哪个类别?
我明白 x,y,z
是绘制数据的坐标,但它对外部标签没有帮助。
这非常难看,它仅适用于具有规则分布的排序数据集。
data = Import["http://ftp.ics.uci.edu/pub/machine-learning-databases/iris/iris.data"];
data = Drop[data, -1]; (*there one extra line at the end*)
inData = data[[All, 1 ;; 4]];
labels = data[[All, 5]];
ListPlot3D[inData,
ColorFunction ->
Function[{x, y, z},
If[y < 0.33, RGBColor[1, 1, 0.],
If[y < 0.66, RGBColor[1, 0, 0.], RGBColor[1, 0, 1]]
]
]
]
预期结果:
最佳答案
假设 points
是坐标列表和 labels
相应标签的列表,例如
points = Flatten[Table[{i, j, Sin[i j]},
{i, 0, Pi, Pi/20}, {j, 0, Pi, Pi/10}], 1];
labels = RandomChoice[{"label a", "label b", "label c"}, Length[points]];
每个标签对应一种颜色,我将其作为规则列表编写,例如
rules = {"label a" -> RGBColor[1, 1, 0],
"label b" -> RGBColor[1, 0, 0], "label c" -> RGBColor[1, 0, 1]};
然后可以用与它们的标签相对应的颜色绘制这些点,如下所示
ListPointPlot3D[Pick[points, labels, #] & /@ Union[labels],
PlotStyle -> Union[labels] /. rules]
编辑
为
ListPlot3D
中的单个点着色您可以使用 VertexColors
, 例如ListPlot3D[points, VertexColors -> labels /. rules, Mesh -> False]
关于wolfram-mathematica - 根据标签在 Mathematica 中绘制着色图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7940766/