Ravi
Ravi

Reputation: 151

Wrong networkx Graph and visulisations are ugly

I have dataframe which consists of more than 60 values of (2x2) size and a sample of some data are as follows

   label           data_pairs
   -1040         [(-1037.13, -1044.77)]
   -1092         [(-1102.64, -1081.64)]
   -1105         [(-1107.36, -1102.64)]
   -137          [(-136.19, -138.75)]
   -1431         [(-1434.08, -1429.31)]
   -612          [(-622.47, -601.98)]
   -672          [(-669.77, -674.95)]
   -752          [(-748.22, -755.9)]
   -791          [(-795.19, -788.02)]

As the name suggests in the datasets, I would like to draw networkx graph for the pair's values and label them accordingly. Based on one of StackOverflow user suggestion, I further modified the codes as per his guidance as follows

df=pd.read_excel (r'C:\Users\xxx\Documents\Python Scripts\Python 
                 Scripts\uuu\data_pairs.xlsx')   # type: datFrame (10,2)

The networkx codes are as follows

import networkx as nx

G = nx.DiGraph()
edges = np.stack(df.Pairs.to_numpy()).reshape(-1,2)   # array of str1248 (5,2)
G = nx.from_edgelist(edges)
pos = nx.spring_layout(G, k=0.6)     # dict

fig=plt.figure(figsize=(10,2),dpi=300)
nx.draw(G, pos=pos, node_size=800, with_labels=True,node_color='y')

edges values looks something like this

[(-1037.13, -1044.77)]  [(-1102.64, -1081.64)]
[(-1107.36, -1102.64)]  [(-1187.16, -1192.42)]
[(-1261.33, -1280.02)]  [(-136.19, -131.06)]
[(-131.06, -138.75)]    [(-136.19, -138.75)]
[(-1434.08, -1429.31)]  [(-304.94, -308.8), (-304.94, -307.85)]

 

This networkx graph I got something like this
this

As you can see the figures looks wrong because of edges values where they are using two values to connect the graph. Also, there is no label shown in graph. For example for data_pairs values [(-1037.13, -1044.77)] I should get label value -1040 in between the line. Something like this (-1037.13)--------------------> (-1044.77) and likewise.
-1040

Above one is just for sake of understanding.

Could you please help me how to get such kind of result? thanks a lot in advance

Upvotes: 1

Views: 751

Answers (1)

yatu
yatu

Reputation: 88226

The problem is that your column contains tuples nested in a list. You need to properly structure the values so that networkX builds the rgaph as expected. A simple way is stacking the values into an array, and reshaping appropriately.

Also, nx.spring_layout has a parameter k, to set the spacing between the nodes in the graph:

from ast import literal_eval
from itertools import chain

df['data_pairs']= df['data_pairs'].map(literal_eval)
G = nx.from_edgelist(chain.from_iterable(df.data_pairs))

plt.figure(figsize=(10,5))
pos = nx.spring_layout(G, k=.6)
nx.draw(G, 
        pos=pos,
        node_size=800, 
        with_labels=True, 
        node_color='y')

enter image description here

Input data -

G.edges()
EdgeView([(-1037.13, -1044.77), (-1102.64, -1081.64), (-1102.64, -1107.36), 
          (-136.19, -138.75), (-1434.08, -1429.31), (-622.47, -601.98), 
          (-669.77, -674.95), (-748.22, -755.9), (-795.19, -788.02), 
          (-304.94, -308.8), (-304.94, -307.85)])

Upvotes: 1

Related Questions