Consider the following

testadj = RandomVariate[BernoulliDistribution[0.15], {50, 50}];
AdjacencyGraph[testadj, VertexSize -> Large]

enter image description here

The nodes are almost completely hidden in the mess of edges. They can be made easier to see by making the edges fade into the background a little.

AdjacencyGraph[testadj, EdgeStyle -> Directive[Opacity[0.4], Gray], 
 VertexSize -> Large]

enter image description here

But we still have the problem that the lines draw over the top of the nodes. Is there a way to force the nodes to draw on top of the lines?

  • $\begingroup$ I have a feeling this has been asked before, or at least came up in a question. I can't find it unfortunately. $\endgroup$ – Szabolcs Jul 26 '12 at 9:11
  • $\begingroup$ Do you need to keep the graph functionality or would it be enough to create a Graphics (not Graph) object that has the vertices on top? $\endgroup$ – Szabolcs Jul 26 '12 at 9:12
  • $\begingroup$ @Szabolcs This one? stackoverflow.com/a/8205128/353410 $\endgroup$ – Dr. belisarius Jul 26 '12 at 12:51
  • $\begingroup$ @belisarius - it's a different problem. $\endgroup$ – Verbeia Jul 27 '12 at 0:07
  • $\begingroup$ @Verbeia Look at Mr's answer :) $\endgroup$ – Dr. belisarius Jul 27 '12 at 0:11

Update: In recent versions, vertices render on top. The rendering order can be controlled as follows:

testadj = RandomVariate[BernoulliDistribution[0.15], {50, 50}];
AdjacencyGraph[testadj, VertexSize -> Large, 
   GraphLayout -> {"RenderingOrder" -> #}, 
   ImageSize -> Medium] & /@ {"EdgeFirst", "VertexFirst"}

enter image description here

This is a nasty hack. It might be the quickest workaround until you find a solution.

testadj = RandomVariate[BernoulliDistribution[0.15], {50, 50}];
gr = AdjacencyGraph[testadj, VertexSize -> Large]

Show[gr, SetProperty[gr, EdgeShapeFunction -> ({} &)]]

Mathematica graphics

The end result is a Graphics object, not a Graph. I am using {} as a "neutral graphics object", something that is accepted inside Graphics, but does not render.

Unfortunately the analogous SetProperty[gr, VertexShapeFunction -> ({} &)] does not seem to work, and I don't understand why. It may have to do something with the fact that the system analyses the vertex shape to make the edges join up nicely to them. If you need to make them disappear, you can use SetProperty[gr, VertexShape -> None].

| improve this answer | |

Here is my implementation by modifying the Box structures.

vertexFirstShow[graph_] :=
    Module[{graphdata, vShow},
           graphdata = ToBoxes[graph];
           vShow = 
                  Cases[graphdata, GraphicsGroupBox[{v_, e_}] :> v, \[Infinity]][[1]]
                       /. {
                           TagBox[DiskBox[pos_, r_], "DynamicName", BoxID -> id_]
                                :> DiskBox[DynamicLocation[id], r],
                           TagBox[StyleBox[DiskBox[pos_, r_], opts__], "DynamicName", BoxID -> id_]
                                :> StyleBox[DiskBox[DynamicLocation[id], r], opts]
           With[{v2 = vShow},
                      graphdata /. GraphicsGroupBox[{v_, e_}] :> GraphicsGroupBox[{v, e, v2}]]

testadj = RandomVariate[BernoulliDistribution[0.15], {50, 50}];

graph = AdjacencyGraph[testadj, VertexSize -> Large, GraphHighlight -> {1, 2, 3}]


enter image description here

It retains a Graph object, and I'm sure the code can be improved to fit more complicated cases.

| improve this answer | |
  • $\begingroup$ Really nice solution, thanks! I accepted @Szabolcs' because it was sufficient for our purposes. $\endgroup$ – Verbeia Jul 27 '12 at 0:07
  • $\begingroup$ @Verbeia His answer is great. It never comes to my mind to use EdgeShapeFunction like that. :) $\endgroup$ – Silvia Jul 27 '12 at 0:12

GraphPlot does alright. Perhaps Inset is the key.

testadj = RandomInteger[BernoulliDistribution[0.15], {50, 50}];

(* gr = graphic *)

GraphPlot[testadj, VertexRenderingFunction -> (Inset[gr, #1] &)]

Mathematica graphics

| improve this answer | |
  • 1
    $\begingroup$ I´d go for the Evolution smiley ;-) $\endgroup$ – Yves Klett Jul 26 '12 at 9:49
  • $\begingroup$ Having our Q&A in two different sites makes us repeat ourselves :) stackoverflow.com/questions/885910/… $\endgroup$ – Dr. belisarius Jul 26 '12 at 15:03
  • $\begingroup$ This is a similar solution but the problem is actually different. $\endgroup$ – Verbeia Jul 27 '12 at 0:07

You can always extract full info from Graph and then use graphics primitives. It is more elaborate but it gives full control.

testadj = RandomVariate[BernoulliDistribution[0.15], {50, 50}];
g = AdjacencyGraph[testadj, VertexSize -> 0];
Show[g, Graphics[{Red, PointSize[Large], 
   Point[AbsoluteOptions[g, VertexCoordinates][[2]]]}]]

enter image description here

| improve this answer | |

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.