12

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.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • 1
    A workaround is the following: MapIndexed[(g = SetProperty[{g, First@#2}, VertexCoordinates -> #1]) &, RandomReal[1, {10, 2}]] – Patrick Stevens Sep 09 '15 at 07:41
  • 1
    @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}]]]. – Szabolcs Sep 09 '15 at 07:59
  • @PatrickStevens I'm confused. The documentation does seem to say index but for me it only works when I used the vertex name. – Szabolcs Sep 09 '15 at 08:16
  • I'm out of my depth here, I'm afraid. I don't have a consistent mental model for how the Graph stuff works. – Patrick Stevens Sep 09 '15 at 08:17
  • @PatrickStevens That's probably because the Graph stuff is not as consistent as the rest of Mathematica. – Szabolcs Sep 09 '15 at 09:17

1 Answers1

8

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.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263