Home > Enterprise >  How to draw a graph with networkx from pandas dataframe with node size depending on value?
How to draw a graph with networkx from pandas dataframe with node size depending on value?

Time:07-29

I have a dataframe:

id1      id2     val
a1       b1      10
a1       b2      4
a1       c1      1
b2       c1      15
c1       a1      3

I want to draw a graph from this dataframe with values from id1 and id2 as nodes. edges go from values from id1 to id2. so edges must go from node a1 to b1, b2, c1. from b2 to c1. from c1 to a1. the size of a node must depend on sum of values from "val" it accepted. so the largest node must be c1 since 1 15 = 16 is largest sum among all id2. Also Id like to attach nodes name to it on graph. How couldI do that?

CodePudding user response:

Use:

import pandas as pd
import matplotlib.pyplot as plt
import networkx as nx

df = pd.DataFrame({'id1': {0: 'a1', 1: 'a1', 2: 'a1', 3: 'b2', 4: 'c1'},
                   'id2': {0: 'b1', 1: 'b2', 2: 'c1', 3: 'c1', 4: 'a1'},
                   'val': {0: 10, 1: 4, 2: 1, 3: 15, 4: 3}})

g = nx.from_pandas_edgelist(df, source="id1", target="id2")

d = df.groupby("id2")["val"].sum().to_dict()
for node in g.nodes:
    d.setdefault(node, 1)

nodes, values = zip(*d.items())
nx.draw(g, nodelist=list(nodes), node_size=[v * 100 for v in values], with_labels=True)
plt.show()

Output

enter image description here

  • Related