12
$\begingroup$

Versions 10.0-10.2 seem to have a bug when trying to set VertexCoordinates twice on a graph (version 9.0 is fine). Can you confirm it? What is a good workaround that will not discard any other attached properties/attributes such as weights?

g = RandomGraph[BarabasiAlbertGraphDistribution[10, 1]]    

(* set coordinates: *)
g = SetProperty[g, VertexCoordinates -> RandomReal[1, {10, 2}]]

(* trying to change them to something else fails *) 
g = SetProperty[g, VertexCoordinates -> RandomReal[1, {10, 2}]]

enter image description here

This related post describes a problem with RemoveProperty: I can't even remove the coordinates.

$\endgroup$
  • 1
    $\begingroup$ A workaround is the following: MapIndexed[(g = SetProperty[{g, First@#2}, VertexCoordinates -> #1]) &, RandomReal[1, {10, 2}]] $\endgroup$ – Patrick Stevens Sep 9 '15 at 7:41
  • 1
    $\begingroup$ @PatrickStevens There are some problems with this: 1. It only works after VertexCoordinates has been already set once. 2. First@#2 should be Extract[VertexList[g], #2]& to deal with situations where the vertex index does not equal the vertex name. But this works: SetProperty[g, VertexCoordinates -> Thread[VertexList[g] -> RandomReal[1, {10, 2}]]]. $\endgroup$ – Szabolcs Sep 9 '15 at 7:59
  • $\begingroup$ @PatrickStevens I'm confused. The documentation does seem to say index but for me it only works when I used the vertex name. $\endgroup$ – Szabolcs Sep 9 '15 at 8:16
  • $\begingroup$ I'm out of my depth here, I'm afraid. I don't have a consistent mental model for how the Graph stuff works. $\endgroup$ – Patrick Stevens Sep 9 '15 at 8:17
  • $\begingroup$ @PatrickStevens That's probably because the Graph stuff is not as consistent as the rest of Mathematica. $\endgroup$ – Szabolcs Sep 9 '15 at 9:17
8
$\begingroup$

A possible workaround is

SetProperty[g, VertexCoordinates -> Thread[VertexList[g] -> RandomReal[1, {10, 2}]]]

Instead of just giving a list, we must provide a list of rules assigning coordinates to each vertex name.

$\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.