John Locke - 4 months ago 57

Python Question

How do I use the eigenvectors corresponding to the two lowest eigenvalues (different from 0) as the x,y coordinates to plot the graph?

I have arrays of eigenvalues and eigenvectors from the laplacian of a graph of nodes. I want to do something similar to the following link that is in MATLAB, but in Python. Also, instead of just the Fielder vector, I also want the next vector larger than the Fielder as well:

https://www.mathworks.com/examples/matlab/mw/matlab-ex64540792-partition-graph-with-laplacian-matrix

Thanks in advance!

Answer

This answer assumes you've already got nodes in a list `nodelist`

and two vectors `vec1`

and `vec2`

which are in the same order.

Networkx uses a dict (usually called `pos`

) for which `pos[u]`

is the (x,y) coordinates of the node `u`

.

```
import networkx as nx
#your code here to define network and find the vectors.
#Please edit a simple version of this code into your question so I can
#add it here.
pos = {node:(x,y) for node, x, y in zip(nodelist, vec1, vec2)}
nx.draw_networkx(G, pos=pos)
```

In defining `pos`

, I've used a dict comprehension and zip.