6
$\begingroup$

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!

enter image description here

enter image description here

$\endgroup$
8
$\begingroup$

GraphPlot supports inputs in several different formats. Originally, it took either a list of Rules 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 Graphs 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"]

enter image description here

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ One of SW's Twitch videos showed that GraphPlot is being updated for version 12.0, so you can expect this to be fixed. $\endgroup$ – Szabolcs Jan 6 '19 at 16:35
1
$\begingroup$

An alternative approach: Use VertexList[g] as the option value for the undocumented option "VertexNames" in GraphPlot.

el = {2 -> 1, 5 -> 0, 7 -> 6, 8 -> 7, 10 -> 8, 0 -> 2, 2 -> 3, 3 -> 4, 5 -> 10, 8 -> 9};

gph = Graph[el, ImageSize -> 300, 
 VertexLabels -> Placed["Name", Center], VertexSize -> Medium, VertexStyle -> White];

Row @ {gph, 
  GraphPlot[gph, ImageSize -> 300, VertexLabeling -> True, 
    "VertexNames" -> VertexList[gph]]}

enter image description here

Note: This approach works for versions older than 12.0. For versions 12.0+, use the function GraphComputation`GraphPlotLegacy to access the older GraphPlot functionality.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.