Shrief Nabil
Shrief Nabil

Reputation: 59

Networkx graph with Python

Suppose you have a list of tuples such as the following: enter image description here

The first element of each tuple represents the person ID and the second represents the skill ID. How can I visualize this data with networkx or any other library?

Thank you

Upvotes: 0

Views: 675

Answers (2)

pakpe
pakpe

Reputation: 5479

You can draw a bipartite graph with networkx:

import networkx as nx
from matplotlib import pyplot as plt

tuples = [(0, 170),
        (0, 165),
        (1, 165),
        (1, 170),
        (2, 150),
        (2, 170),
        (3, 150),
        (3, 165),
        (4, 170),
        (4, 150)]
left_nodes = [tup[0] for tup in tuples]

G = nx.Graph()
G.add_edges_from(tuples)
pos = nx.bipartite_layout(G, nodes= left_nodes)
node_colors = ["turquoise" if node in left_nodes else "pink" for node in G.nodes]
nx.draw(G,pos=pos, with_labels=True, node_color = node_colors)
plt.show()

enter image description here

Upvotes: 2

I'mahdi
I'mahdi

Reputation: 24049

you can use sankey diagrams like below:

import holoviews as hv
import plotly.graph_objects as go
import plotly.express as pex
import pandas as pd
  
tuples = [(0, 170),
        (0, 165),
        (1, 165),
        (1, 170),
        (2, 150),
        (2, 170),
        (3, 150),
        (3, 165),
        (4, 170),
        (4, 150)]

df = pd.DataFrame.from_records(tuples, columns =['id', 'skill'])
df = df.assign(v=pd.Series([1]*len(tuples)).values)

hv.extension('bokeh')
hv.Sankey(df, kdims=["id","skill"])

output: enter image description here

Upvotes: 1

Related Questions