TransWikia.com

How to align a network graph to an image?

Mathematica Asked on July 9, 2021

An arbitrary example:

Create a random image

Image[RandomReal[{0, 1}, {10, 10}], ImageSize -> Medium]

enter image description here

Define some points and a graph to align

c = PixelValuePositions[%, White, .1]
RandomGraph[Table[Length[c], 2]]

enter image description here

Out[1]: {{2, 9}, {1, 8}, {1, 7}, {2, 7}, {9, 6}, {2, 2}}

How can I align the nodes of a graph to coordinates on an image?

Ideally this would be a graph from a correlation matrix

RandomReal[{-1, 1}, Table[Length[c], 2]] // MatrixForm

One Answer

SeedRandom[1]
img = Image[RandomReal[{0, 1}, {10, 10}], ImageSize -> Medium];
c = PixelValuePositions[img, White, .1];

rg = RandomGraph[Table[Length[c], 2], ImageSize -> Medium];

rg2 = SetProperty[rg, 
   {VertexCoordinates -> c - 1/2, EdgeStyle -> Directive[Red, Thick]}];

Row[{rg, Show[img, rg2]}]

enter image description here

Correct answer by kglr on July 9, 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