merged_table=
ID 0 1 2 3 4 5 6 7 8 9 10 11 12
131X1319 1 14 6 16 1 10 8 15 15 17 15 18 16
13212YX3 1 1 4 8 11 9 14 7 0 3 0 17 13
13216131 1 1 13 9 15 17 0 9 3 15 11 8 10
132921W6 1 14 10 4 18 7 8 15 15 17 15 18 16
132W22YY 0 13 12 9 15 5 0 13 3 15 11 15 6
132X219Y 16 5 8 11 15 3 6 2 13 6 4 14 10
132Y231B 17 18 17 17 19 18 20 15 15 17 15 18 16
I have a data frame like this and I have constructed a graph, in which the edges get thicker if two nodes occur more than once. This is the graph I have generated:
columns=list(merged_table.columns.values)
ct = Counter(p for i in range(len(columns)-2)
for p in zip(merged_table[columns[i]],merged_table[columns[i 1]]))
pattern_g = nx.DiGraph()
pattern_g.add_edges_from(ct)
width = [ct[p] for p in pattern_g.edges]
nx.draw(pattern_g, node_color = 'orange', with_labels=True, width = width)
plt.show()
Now, I want to draw subgraphs of nodes that have a thicker edge in the graph. if the flow of the nodes is more than two it's better. I tried using strongly_connected_componenets but it is printing all the nodes and edges of the graph.
CodePudding user response:
I assume you have in mind some kind of side-by-side comparison. With that in mind, here's a modification of your script that produces drawings of both graphs with the same layout.
columns=list(merged_table.columns.values)
ct = Counter(p for i in range(len(columns)-2)
for p in zip(merged_table[columns[i]],merged_table[columns[i 1]]))
pattern_g = nx.DiGraph()
pattern_g.add_edges_from(ct)
# get layout for use in both graph drawings
pos = nx.spring_layout(pattern_g)
# Draw first graph, using frequency of each pair as edge-width
width = [ct[p] for p in pattern_g.edges]
nx.draw(pattern_g, pos = pos, node_color = 'orange', with_labels=True, width = width)
plt.show()
# Build and draw second graph, excluding edges with wgts below threshold 'thr'
thr = 1
pattern_subg = nx.DiGraph()
for e,wgt in ct.items():
if wgt>thr:
pattern_subg.add_edge(*e)
width = [ct[p] for p in pattern_subg.edges]
nx.draw(pattern_subg, pos = pos, node_color = 'orange', with_labels=True, width = width)
plt.show()