Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
I have a feeling this has been asked before, or at least came up in a question. I can't find it unfortunately. – Szabolcs Jul 26 '12 at 9:11
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? – Szabolcs Jul 26 '12 at 9:12
@Szabolcs This one? – Dr. belisarius Jul 26 '12 at 12:51
@belisarius - it's a different problem. – Verbeia Jul 27 '12 at 0:07
@Verbeia Look at Mr's answer :) – Dr. belisarius Jul 27 '12 at 0:11
up vote 6 down vote accepted

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].

share|improve this answer

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

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

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.

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

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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