A very peculiar thing is happening when I wrap a Graph with GraphPlot. The picture below shows the correct vertex labeling. However, GraphPlot changes the vertex labeling! Am I misusing this function or is this something I should report to Wolfram? I'm running version 11.0.1.0.
Below is some code to reproduce the issue (but with a simpler graph).
{gph = Graph[{2 -> 1, 5 -> 0, 7 -> 6, 8 -> 7, 10 -> 8, 0 -> 2, 2 -> 3,
3 -> 4, 5 -> 10, 8 -> 9}, VertexLabels -> "Name"],
GraphPlot[gph, VertexLabeling -> True]}
Interestingly enough, using a built-in function (like KaryTree) yields expected results:
{KaryTree[15, VertexLabels -> "Name"],
GraphPlot[KaryTree[15], VertexLabeling -> True]}
My work-around is using Graph and formatting it exactly how I want, but I would like to understand why the vertex labeling is incorrect!
Answer
GraphPlot
supports inputs in several different formats. Originally, it took either a list of Rule
s or an adjacency matrix. With a rule-list input, it displays the vertex labels you would expect. But the adjacency matrix does not contain information about vertex names. Thus with a matrix input, it uses vertex indices (not names) for labelling.
In 11.3 (and probably all the way back to at least 10.0), GraphPlot
also takes Graph
expressions as input. But it handles Graph
s by converting them to a matrix (not a rule list!) first. Thus the vertex names are lost, and it will use vertex indices instead.
Workaround: convert the graph to a rule list yourself.
GraphPlot[Rule @@@ EdgeList@gph, VertexLabeling -> True,
DirectedEdges -> True, Method -> "LayeredDrawing"]
Comments
Post a Comment