30
$\begingroup$

What is the difference (in purpose) between Graph and GraphPlot? Which function is bested suited to which tasks?

Background: I just spent the last two hours trying to make this graph readable:

trans = Uncompress@
   "1:eJyVV01vwjAMRfu47U/sv3RKKc2KGsahxw2YOE0aXPff17RJSZD8/HIBga3Efn5+\
dl4/f/rT32q1ujyNH+35cj09+F8v40d1/j1+XY+\
Ht8P30T2Pf9ihbyvn7Y0T3GbjzZtyM5LbYmcP9Els++FdTWJy7EwrOEYzTjbeRkHyODnvB\
adgnb7d8KHi4e3OgBudmb5sB3xsV1CBOTLpymCdv1sp/\
mBlzhq9tJrfqolRC9aSDNSzGPwxFv5f05GAldDVoJKbrgBWJsnCIuFMB/\
28JVXNMR7IQsfVvZSTCD07V79Z78Clo1VLIS+YVn5/2H67Q3h4M83wZr2B4W/\
oCmhaFDXSWysUWcVpH4sElyUTFYsEGxnPRl7nlRgTFoFBu0xGjuDToRLAs5GeeDWqes22H\
ORPLDl/FENXLXCmuVWQanrkBwmT88uGjhh8JgIyYRI91EW4aBoqtOIbg60RxD+IAGjuZC+\
QRSeXfC2qkhGor6gqD60WfdoZTOy8SGu9iONKFEJeVO7eELQGMvOD0UDYaXHN5a6DT5Zlv\
GuYlu2TmhIyD726bP5gSnswmEttWR9puG1wH6WE1VuSBI2sVNA6cfxlK6JIx7utFDE7vI5\
lyAL+Kv09FopgpA9t+\
cIcCcjZMXgYV4YWrHd86DFyR2xpTh012aCEY8sSoy3sX3A0OHUHTjWYWgR8fv/wd/kB";
Graph[DeleteDuplicates@trans, VertexLabels -> Placed["Name", Center], 
 VertexShapeFunction -> "Capsule", VertexSize -> 1, 
 VertexLabelStyle -> Large, 
 EdgeShapeFunction -> 
  GraphElementData["ShortUnfilledArrow", "ArrowSize" -> 0.02]]

Unreadable

Then I discovered that GraphPlot produces much more readable results:

GraphPlot[
 DeleteDuplicates@trans /. (DirectedEdge[a_, b_] -> Rule[a, b]), 
 VertexLabeling -> True, DirectedEdges -> True]

Readable

which prompted me to wonder: what's the point of Graph?

Of course I noticed this, while related, which asks about the Combinatorica package.

$\endgroup$
1
  • 3
    $\begingroup$ Graph is not simply a way to plot graphs. It is a whole new data type on which you can perform lots of operations. It just happens to be displayed as its visualization. $\endgroup$
    – Szabolcs
    Jun 4, 2012 at 18:38

1 Answer 1

27
$\begingroup$

First of all Graph is more recent functionality. Now try this:

GraphPlot[{1 -> 2}] // Head

Graphics

and then this

Graph[{1 -> 2}] // Head

Graph

Basically while GraphPlot and GraphPlot3D both return pure graphic objects, Graph returns true graph which contains all information about the graph and can be computed with. For example:

g=Graph[{1->2}];
 AdjacencyMatrix[g] // Normal

{{0, 1}, {0, 0}}

Because they can be computed with, there is wide functionality built around Graph objects which GraphPlot does not have - from graph theory to visualization and styling.

Also to get more feel for the difference you can try AtomQ, which is the function that yields True if expression cannot be divided into subexpressions, and yields False otherwise, - as paraphrased from form Documentation Center.

AtomQ[g]

True

Compare with

 AtomQ[GraphPlot[{1 -> 2}]]

False

Which can be also hinted from:

 g // InputForm

Graph[{1, 2}, {DirectedEdge[1, 2]}]

and this:

 GraphPlot[{1 -> 2}] // InputForm

Graphics[Annotation[GraphicsComplex[{{1., 0.}, {0., 8.979318433952318*^-11}}, {{RGBColor[0.5, 0., 0.], Line[{{1, 2}}]}, {RGBColor[0, 0, 0.7], Tooltip[Point1, 1], Tooltip[Point2, 2]}}, {}], VertexCoordinateRules -> {{1., 0.}, {0., 8.979318433952318*^-11}}], FrameTicks -> None, PlotRange -> All, PlotRangePadding -> Scaled[0.1], AspectRatio -> Automatic]

Good thing to know is that built-in GraphData return Graph object on default:

GraphData /@ GraphData["Hypotraceable"]

enter image description here

And another cool consequence of the above is that you can even copy and paste the visual representations returned by Graph and compute them:

enter image description here

Besides GraphData there are many other functions that return Graph objects, such as CompleteGraph, GraphComplement, CayleyGraph, Subgraph and so on.

Another thing to know is that Graph objects have their own right-click menu alowing one to deal with the graph interactively (sample workflow is shown below):

GraphData["IcosahedralGraph", "LineGraph"]

enter image description here

$\endgroup$
3
  • 8
    $\begingroup$ LayeredGraphPlot still often produces much better results than the equivalent Graph-based solution. Fortunately GraphPlot does work with Graph objects---you might want to mention this! $\endgroup$
    – Szabolcs
    Jun 4, 2012 at 18:40
  • $\begingroup$ @Szabolcs Yes, thanks, I surely will add more info, - gathering it ;-) In meantime if anyone want's to see something relative added in this post - please comment. $\endgroup$ Jun 4, 2012 at 18:48
  • 1
    $\begingroup$ @VitaliyKaurov your answer covers what is the purpose of defining and using a graph object. But in terms of Graph visualization I see there is significant overlap between Graph and GraphPlot. The name "GraphPlot" suggests that this function specializes on plotting the graph. Perhaps another answer should highlight what are the advantages of plotting, visualizing graphs with GraphPlot, instead of Graph. And isn't it possible to merge functionality of GraphPlot into Graph ? I have also noticed that GraphPlot drawing is part of Graph Visualization. $\endgroup$ Mar 5, 2016 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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