作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有没有办法从GraphPlot生成的图形的(FullForm或InputForm)中抽象出GraphPlot适用于VertexCoordinate Rules的顶点顺序?我不想使用 GraphUtilities 函数 VertexList。我也知道 GraphCoordinates,但是这两个函数都适用于图形,而不是 GraphPlot 的图形输出。
例如,
gr1 = {1 -> 2, 2 -> 3, 3 -> 4, 4 -> 5, 5 -> 6, 6 -> 1};
gp1 = GraphPlot[gr1, Method -> "CircularEmbedding",
VertexLabeling -> True];
Last@(gp1 /. Graphics[Annotation[x___], ___] :> {x})
Needs["GraphUtilities`"];
gr2 = SparseArray@
Map[# -> 1 &, EdgeList[{2 -> 3, 3 -> 4, 4 -> 5, 5 -> 6}]];
VertexList[gr2]
gp2 = GraphPlot[gr2, VertexLabeling -> True,
VertexCoordinateRules ->
Thread[VertexList[gr1] ->
Last@(gp1 /. Graphics[Annotation[x___], ___] :> {x})[[2]]]];
Last@(gp2 /. Graphics[Annotation[x___], ___] :> {x})
VertexList@
SparseArray[
Map[# -> 1 &, EdgeList[{2 -> 3, 3 -> 4, 4 -> 5, 5 -> 6}]], {6, 6}]
最佳答案
对于顶点标记,一种方法是获取标签的坐标。请注意,GraphPlot 的输出在 GraphicsComplex 中,其中坐标别名的坐标作为第一个标签,您可以将其作为
points = Cases[gp1, GraphicsComplex[points_, __] :> points, Infinity] // First
FullForm
你会看到标签在文本对象中,将它们提取为
labels = Cases[gp1, Text[___], Infinity]
actualLabels = labels[[All, 1, 1]];
coordAliases = labels[[All, 2]]
actualCoords = points[[coordAliases]]
getLabelCoordinateMap[gp1_] :=
Module[{points, labels, actualLabels, coordAliases, actualCoords},
points =
Cases[gp1, GraphicsComplex[points_, __] :> points, Infinity] //
First;
labels = Cases[gp1, Text[___], Infinity];
actualLabels = labels[[All, 1, 1]];
coordAliases = labels[[All, 2]];
actualCoords = points[[coordAliases]];
Thread[actualLabels -> actualCoords]
];
getLabelCoordinateMap[gp1]
graphName = {"Grid", {3, 3}};
gp1 = GraphPlot[Rule @@@ GraphData[graphName, "EdgeIndices"],
VertexCoordinateRules -> GraphData[graphName, "VertexCoordinates"],
VertexLabeling -> True]
gp2 = GraphPlot[GraphData[graphName, "AdjacencyMatrix"],
VertexCoordinateRules -> GraphData[graphName, "VertexCoordinates"],
VertexLabeling -> True]
edges2mat[edges_] := Module[{a, nodes, mat, n},
(* custom flatten to allow edges be lists *)
nodes = Sequence @@@ edges // Union // Sort;
nodeMap = (# -> (Position[nodes, #] // Flatten // First)) & /@
nodes;
n = Length[nodes];
mat = (({#1, #2} -> 1) & @@@ (edges /. nodeMap)) //
SparseArray[#, {n, n}] &
];
mat2edges[mat_List] := Rule @@@ Position[mat, 1];
mat2edges[mat_SparseArray] :=
Rule @@@ (ArrayRules[mat][[All, 1]] // Most)
关于wolfram-mathematica - GraphPlot 图形中的 VertexCoordinate Rules 和 VertexList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4245946/
有没有办法从GraphPlot生成的图形的(FullForm或InputForm)中抽象出GraphPlot适用于VertexCoordinate Rules的顶点顺序?我不想使用 GraphUtil
我是一名优秀的程序员,十分优秀!