0

I use Networkx in Python to generate the minimum spanning tree of the correlation in my data, and I want to find the (x,y) coordinates of the nodes. I read this question : Exporting Layout Positions for a Graph Using NetworkX and tried to apply it, but I don't really understand why I don't get the expected result.

Here is the code I use :

import networkx as nx
import random as rn
import pylab as pl
def create_tree(data):
    corr_matrix = define_correlation_matrix(data)
    G = nx.Graph(corr_matrix)
    pos = nx.minimum_spanning_tree(G)
    rn.seed = 5
    colors = 'bcgmry'
    components = nx.connected_components(pos)
    for i in components:
        component = pos.subgraph(i)
        nx.draw_graphviz(component,
                         node_color = colors[rn.randint(0,   len(colors)-1)], 
                         node_size = 15,
                         edge_color = [corr_matrix[i][j]*0.5 for (i,j) in component.edges()],
                         with_labels = True,
                         labels = dict([(x,data[x]) for x in component.nodes()]))
    nx.set_node_attributes(G,'pos',pos)
    print G.node
    pl.show()

But instead of having the coordinates I get this :

{0: {'pos': <networkx.classes.graph.Graph object at 0x10513ea50>}, 1: {'pos': <networkx.classes.graph.Graph object at 0x10513ea50>}, 2: {'pos': <networkx.classes.graph.Graph object at 0x10513ea50>}, 3: {'pos': <networkx.classes.graph.Graph object at 0x10513ea50>}, 4: {'pos': <networkx.classes.graph.Graph object at 0x10513ea50>}, 5: {'pos': <networkx.classes.graph.Graph object at 0x10513ea50>}}

Do you know how I can change the code to have the coordinates ?

Thank you.

Community
  • 1
  • 1
Kopii
  • 1
  • 1
  • In the answer you're copying from it's `pos=nx.spring_layout(G)` so it's a dict giving coordinates of nodes. In your code you've defined `pos` to be a minimum spanning tree (so it's a graph). – Joel Aug 10 '16 at 09:42
  • Yes, I saw when I printed the result that it was a graph, but do you think it's possible to convert this graph into a dict of coordinates ? I also tried using `nx.to_dict_of_dicts(pos)` and `nx.to_numpy_matrix(pos)` but then I don't know how to get the coordinates. – Kopii Aug 10 '16 at 09:57
  • First give the graph a name other than `pos`. `T` (for tree) might be a good choice. `pos` is generally used to give the coordinates of nodes in the graph. You need to recognize that the graph and the coordinates of the nodes in the graph are different things. – Joel Aug 10 '16 at 21:43
  • Then try following the answer you said you were following. The fact that that answer used the command `nx.spring_layout` is kind of key. If you try other commands that aren't designed to create the layout, it won't work. – Joel Aug 10 '16 at 21:49
  • Oh, but I used the `nx.spring_layout` to find the nodes, before posting the question, but when I plot the given coordinates, it looks really different from what I had after the `nx.draw_graphviz`. So it's not possible to have the same ? Thank you for your help. – Kopii Aug 11 '16 at 03:19

0 Answers0