TransWikia.com

NearestNeighborGraph doesn't show a 3D graph when one or more points are Integer and the rest are Decimal?

Mathematica Asked by Dale O'Brien on May 6, 2021

Bug introduced in 10.3 or earlier and persisting through 12.1.1


When all the points are the same kind, say Integer then NearestNeighborGraph will display a 3D graph when using {x,y,z} coordinates. However, when just one of those points is say a Decimal instead then one sees a 2D graph. It seems the reverse is true too. For example:

allIntegers = NearestNeighborGraph[{{0,0,0},{-1,1,1},{1,-1,1},{-1,-1,1},{1,1,1},{0,0,2},{0,0,1}},{All,2}];
allDecimals = NearestNeighborGraph[{{0.,0.,0.},{-1.,1.,1.},{1.,-1.,1.},{-1.,-1.,1.},{1.,1.,1.},{0.,0.,2.},{0.,0.,1.}},{All,2}];

oneInteger = NearestNeighborGraph[{{0,0.,0.},{-1.,1.,1.},{1.,-1.,1.},{-1.,-1.,1.},{1.,1.,1.},{0.,0.,2.},{0.,0.,1.}},{All,2}];
oneDecimal = NearestNeighborGraph[{{0.,0,0},{-1,1,1},{1,-1,1},{-1,-1,1},{1,1,1},{0,0,2},{0,0,1}},{All,2}];

Row[{allIntegers, allDecimals, oneInteger, oneDecimal}]

Displays:

2 3D graphs and 2 2D graphs

Is there something I’m doing wrong or some parameter I need to pass? I’d like to be able to force 3D.

One Answer

One possible workaround to this bug is to set VertexCoordinates explicitly.

pts = {{0., 0, 0}, {-1, 1, 1}, {1, -1, 1}, {-1, -1, 1}, {1, 1, 1}, {0, 0, 2}, {0, 0, 1}};

NearestNeighborGraph[pts, {All, 2}, 
   VertexCoordinates -> pts (* <-- workaround *)]

Correct answer by Szabolcs on May 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP