0

I am new to graph analysis and I need some hints on how to visualize my graph.

I have a pandas Dataframe that contains my edges with a distance.

Here is a sample of my Dataframe :

    node_1, node_2, distance
1    nodeA,  nodeB,     12.5 
2    nodeA,  nodeA,        1 
3    nodeB,  nodeA,     10.6 
4    nodeC,  nodeD,       20 
5    nodeD,  nodeE,       25 
...    ...     ...       ...

In my real dataset, I have around 400 nodes and 1000 edges.

Using library networkx, I have been able to draw my graph :

G = nx.from_pandas_edgelist(df = edges, source = 'node_1', target = 'node_2', edge_attr='distance')
G.add_nodes_from(nodes_for_adding = edges['node_1'].tolist())

edge_all = [(u,v) for (u,v,d) in G.edges(data=True)]

pos=nx.spring_layout(G)
plt.figure(figsize=(40,30))
nx.draw_networkx_nodes(G,pos,node_size=10)                 # draw nodes
nx.draw_networkx_edges(G,pos,edgelist=edge_all,width=0.08) # draw edges

enter image description here

Luckily, I know what my graph should look like. It should be a bunch of cluster of nodes linked together by some independant nodes. (A cluster being defined by lot of connection with low distance)

What would be a the good direction to represent my graph as I want ?

The goal of this visualization would be to then map some routes on it to see how it goes from some clusters to some other.

-- EDIT --

Here is the output using method proposed by Paul Brodersen :

Partitioning was done using Spectral Clustering with 8 clusters based on distance of edges

enter image description here

Nakeuh
  • 1,757
  • 3
  • 26
  • 65
  • 1
    [This](https://stackoverflow.com/a/43541777/2912349) may be a starting point. – Paul Brodersen Feb 26 '20 at 11:53
  • This looks like what I am trying to do, thanks ! I used your solution using custom partitions (Spectral Clustering based on the distance between nodes, since it's a weighted graph). The positioning looks great but weirdly do not match the coloring of nodes (I added the new visualization in the post) I'll try to improve that. – Nakeuh Feb 27 '20 at 10:23
  • Can you post the code and link the data you used to create the figure? Hard to debug otherwise. – Paul Brodersen Feb 27 '20 at 11:55
  • You can find here (https://drive.google.com/drive/folders/1r7Bl9BNxcT_XauW3_2ZjlamDInoDn-HI) a notebook with the dataset – Nakeuh Feb 27 '20 at 13:40
  • I finally found out what my mistake was. The order of my nodes out of my partitioning did not match the order of the nodes of my graph. Using the `nodelist` parameter in the `nx.draw()` call, I managed to specify my nodes correctly. Thanks for your help ! – Nakeuh Feb 28 '20 at 11:53
  • Glad to hear it! – Paul Brodersen Feb 28 '20 at 12:49

0 Answers0