1

I have a question regarding how to create a network with the following characteristics:

A            B
12     [32, 12, 45]
2      [43, 41 ,2,15]
15     [15,18,24,1]
1      [1,24,7]

where numbers in column A and numbers in B are nodes. I would like to find the connection between A and their corresponding values in B; then, connect elements in B to A or to each other. For example:

  • 12 is linked with itself (isolated since no other node is linked with it);
  • 2 is linked with itself and 15;
  • 15 is linked with itself and 1 and 2;
  • 1 is linked with itself (isolated)

How can I plot it?

I think I should use explode; I am using the wrong approach for edgelist:

G = nx.from_pandas_edgelist(df, 'A', 'B')
plt.figure(3,figsize=(30,24)) 
nx.draw(G, node_size=200, with_labels=False)
pos = nx.spring_layout(G) 
nx.draw(G, pos = pos)
halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

0

I have very little experience with this type of graph, but I've implemented the list extension with explode() and created a graph in matplotlib with columns A and B as paths.

import pandas as pd
import numpy as np
import io

df = pd.DataFrame({'A':[12,2,15,1], 'B':[[32, 12, 45], [43,41,2,15], [15,18,24,1], [1,24,7]]})
df = df.explode('B', ignore_index=True)

import matplotlib.pyplot as plt
import networkx as nx

G = nx.DiGraph()
nx.add_path(G, df['A'])
nx.add_path(G, df['B'])

nx.draw_networkx(G)
plt.show()

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • May I ask you if for the direction you are using the first column, i.e. `df['A']`? For example, `12` is connected to `32` and `45` (outgoing arrows). `15` is connected to `18,24,1` (outgoing); since `2` is connected to `[43, 41,15]`, `15` has also an ingoing arrow. I do not know if it makes sense. Looking at `24`, there is no link with `15` and it seems strange –  Aug 15 '20 at 22:00
  • I don't have the knowledge to reply to your comment. I think it's one thing to ask again with this code. There are many experienced experts here. – r-beginners Aug 16 '20 at 01:31
0

Based on your requirement, you will need to not only use explode by also filter the data for the unique set of nodes that you want to retain (else it will plot all the nodes available).

Requirements -

  • 12 is linked with itself (isolated since no other node is linked with it);
  • 2 is linked with itself and 15;
  • 15 is linked with itself and 1 and 2;
  • 1 is linked with itself (isolated? - Not really, 15 connects to 1)

You can do the following to process the data -

df = pd.DataFrame({'A':[12,2,15,1], 'B':[[32, 12, 45], [43,41,2,15], [15,18,24,1], [1,24,7]]})
df = df.explode('B')

vocab = set(df['A'].unique()).intersection(set(df['B'].unique()))
df = df[df['B'].isin(vocab)]
print(df)
#leaving only the nodes that have existing connections.
    A   B
0  12  12
1   2   2
1   2  15
2  15  15
2  15   1
3   1   1

Then you can simply use networkx to plot from the Source-Target dataframe.

G = nx.from_pandas_edgelist(df, 'A', 'B')
nx.draw_networkx(G)
plt.show()

enter image description here

Checking for self-loops -

print("Nodes with self loops:",list(G.nodes_with_selfloops()))
Nodes with self loops: [12, 2, 15, 1]

If you want to implement plots WITH self loops, then you will need to use Graphviz to plot it as mentioned on this SO post

Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51