I would like to draw a given graph $G$ in a specific way and I would like to know whether Mathematica can do it in a (semi-)automated way.

My goal is to draw the graph $G$ in 2D, fixing all the $x$ coordinates of its vertices and letting the $y$ coordinates be arranged as they would be arranged by already existing layouts such as "SpringElectricalEmbedding" or "SpringEmbedding" (taking obviously in account the already fixed $x$ coordinates).

Q1) Is there a way to do this in Mathematica, possibly using one of the sub-options of "SpringElectricalEmbedding" or "SpringEmbedding" (e.g. "EnergyControl")?

Q2) If the answer to the first question is negative, how would one build a function that, given the graph $G$ and the $x$ coordinates of its vertices, computes the $y$ coordinates to obtain such force-directed layout?

  • 2
    $\begingroup$ The other way around is possiblel (letting Mma set a layout and the re-setting the x coordinate), I think . However, if you want to first set the x coordinates I doubt you can get Mma to recalculate the ys $\endgroup$ Dec 1, 2014 at 17:46

1 Answer 1


You can use GraphLayout to arrange your Vertexes by SpringEmbedding or other function, then extract the full two-dimensional locations of the vertexes, then assign the x-coordinate of each vertex to your known or desired x coordinates and re-display the graph with these new vertex locations.

 g = CompleteGraph[20, GraphLayout -> "SpringElectricalEmbedding"];

Basic complete graph

myVertexCoordinates = VertexCoordinates /. 
 AbsoluteOptions[g, VertexCoordinates][[1]];

myFixedXCoordinates = Table[RandomReal[],

myNewVertexCoordinates =  Table[
  myVertexCoordinates[[i, 2]]}, 
  {i, Length[myFixedXCoordinates]}];

Graph[g, VertexCoordinates -> myNewVertexCoordinates]

Simple complete graph with fixed coordinates


Your Answer

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

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