Message Boards Message Boards

1
|
5530 Views
|
1 Reply
|
1 Total Likes
View groups...
Share
Share this post:

Setting vertex coordinates on a graph

Posted 7 years ago

The following bug caused me some headache, so I thought I would share the workaround.

Sometimes trying to set VertexCoordinates on a graph failed mysteriously, at other times it worked fine. I could not figure out why. It turns out that it does not like packed arrays.

This fails:

g = GraphData[{"BananaTree", {4, 5}}];
SetProperty[g, VertexCoordinates -> RandomReal[1, {21, 2}]]

This works:

SetProperty[g, 
 VertexCoordinates -> 
  Developer`FromPackedArray@RandomReal[1, {21, 2}]]

Update: This does not work either. I did not pay attention. The command does not "fail", but the coordinates are not set.

The following really does work:

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

I am not sure why this is necessary as even the first command works just fine on RandomGraph[{21,50}]. Now you can see how frustrating it is to try to write robust packages that use graphs. You never know when you'll bump into yet another weird failure.

POSTED BY: Szabolcs Horvát
Posted 7 years ago

As the documentation of VertexCoordinates,it is a bug indeed presisting many version.

Mathematica graphics

The method based on Thread always is my choice currently.

Graph[g, VertexCoordinates -> Thread[VertexList[g] -> RandomReal[1
POSTED BY: Yode Japhe
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract