Social network analysis treats people or entities as nodes and their relationships as edges. With NetworkX, you can build these graphs, compute useful network metrics, and visualize the structure to better understand influence, connectivity, and bridge relationships. This tutorial shows how to create a simple social graph, measure centrality, visualize important nodes, and interpret the results in a practical example. ### Building a Simple Social Network Let's start by creating a small social network and inspecting its structure.
import networkx as nx
G = nx.Graph()
G.add_edges_from(
[
("Alice", "Bob"),
("Alice", "Charlie"),
("Bob", "David"),
("Charlie", "David"),
("Charlie", "Eve"),
("David", "Eve"),
("Eve", "Frank"),
]
)
print("Number of nodes:", G.number_of_nodes())
print("Number of edges:", G.number_of_edges())
print("Neighbors of Charlie:", list(G.neighbors("Charlie")))Number of nodes: 6 Number of edges: 7 Neighbors of Charlie: ['Alice', 'David', 'Eve']
- Each node represents a person. - Each edge represents a connection or relationship. - Even this simple structure already lets you ask useful questions about who is connected to whom. ### Measuring Degree Centrality Degree centrality measures how connected a node is relative to the rest of the graph. In a social network, this often highlights highly connected people.
import networkx as nx
G = nx.Graph()
G.add_edges_from(
[
("Alice", "Bob"),
("Alice", "Charlie"),
("Bob", "David"),
("Charlie", "David"),
("Charlie", "Eve"),
("David", "Eve"),
("Eve", "Frank"),
("Frank", "Grace"),
("Grace", "Helen"),
]
)
degree_centrality = nx.degree_centrality(G)
print("Degree centrality:")
for user, value in sorted(degree_centrality.items(), key=lambda x: x[1], reverse=True):
print(f"{user}: {value:.3f}")Degree centrality: Charlie: 0.429 David: 0.429 Eve: 0.429 Alice: 0.286 Bob: 0.286 Frank: 0.286 Grace: 0.286 Helen: 0.143
- **`nx.degree_centrality(G)`** returns a normalized score for each node. - Higher values mean the node has more direct connections. ### Measuring Betweenness Centrality Betweenness centrality identifies nodes that sit on many shortest paths between other nodes. These nodes often act as bridges between groups.
import networkx as nx
G = nx.Graph()
G.add_edges_from(
[
("Alice", "Bob"),
("Alice", "Charlie"),
("Bob", "David"),
("Charlie", "David"),
("Charlie", "Eve"),
("David", "Eve"),
("Eve", "Frank"),
("Frank", "Grace"),
("Grace", "Helen"),
]
)
betweenness = nx.betweenness_centrality(G)
print("Betweenness centrality:")
for user, value in sorted(betweenness.items(), key=lambda x: x[1], reverse=True):
print(f"{user}: {value:.3f}")Betweenness centrality: Eve: 0.571 Frank: 0.476 Grace: 0.286 Charlie: 0.214 David: 0.214 Alice: 0.024 Bob: 0.024 Helen: 0.000
- Nodes with high betweenness often connect otherwise separate parts of the network. - In real networks, these can represent coordinators, brokers, or information bottlenecks. ### Visualizing the Network with Node Sizes A network diagram becomes more informative when visual styling reflects graph metrics.
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edges_from(
[
("Alice", "Bob"),
("Alice", "Charlie"),
("Bob", "David"),
("Charlie", "David"),
("Charlie", "Eve"),
("David", "Eve"),
("Eve", "Frank"),
("Frank", "Grace"),
("Grace", "Helen"),
("Frank", "Helen"),
]
)
degree_centrality = nx.degree_centrality(G)
node_sizes = [4000 * degree_centrality[node] + 300 for node in G.nodes()]
pos = nx.spring_layout(G, seed=42)
plt.figure(figsize=(10, 7))
nx.draw(
G,
pos,
with_labels=True,
node_size=node_sizes,
node_color="lightblue",
edge_color="gray",
font_size=10,
)
plt.title("Social Network with Node Size by Degree Centrality")
plt.axis("off")
plt.show()- Larger nodes represent more connected people. - This makes central figures easier to identify visually than from a table alone. ### Coloring Nodes by Betweenness You can also color nodes by a metric to show which people connect different parts of the network.
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edges_from(
[
("Alice", "Bob"),
("Alice", "Charlie"),
("Bob", "David"),
("Charlie", "David"),
("Charlie", "Eve"),
("David", "Eve"),
("Eve", "Frank"),
("Frank", "Grace"),
("Grace", "Helen"),
("Frank", "Helen"),
("Helen", "Ivan"),
]
)
betweenness = nx.betweenness_centrality(G)
node_colors = [betweenness[node] for node in G.nodes()]
node_sizes = [5000 * betweenness[node] + 400 for node in G.nodes()]
pos = nx.spring_layout(G, seed=12)
plt.figure(figsize=(10, 7))
nx.draw(
G,
pos,
with_labels=True,
node_color=node_colors,
node_size=node_sizes,
cmap=plt.cm.viridis,
edge_color="lightgray",
font_size=10,
)
plt.title("Social Network Colored by Betweenness Centrality")
plt.axis("off")
plt.show()- Nodes with stronger colors and larger sizes have higher betweenness. - These are often the key connectors between subgroups. ### Practical Example: Finding Influential and Bridging Users Here is a practical example that combines multiple metrics and interprets the result.
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edges_from(
[
("Alice", "Bob"),
("Alice", "Cara"),
("Bob", "Dina"),
("Cara", "Dina"),
("Cara", "Evan"),
("Dina", "Farah"),
("Evan", "Farah"),
("Farah", "Gabe"),
("Gabe", "Hana"),
("Hana", "Iris"),
("Farah", "Iris"),
]
)
degree_centrality = nx.degree_centrality(G)
betweenness = nx.betweenness_centrality(G)
print("Top 3 by degree centrality:")
for user, value in sorted(degree_centrality.items(), key=lambda x: x[1], reverse=True)[:3]:
print(f"{user}: {value:.3f}")
print("\nTop 3 by betweenness centrality:")
for user, value in sorted(betweenness.items(), key=lambda x: x[1], reverse=True)[:3]:
print(f"{user}: {value:.3f}")
pos = nx.spring_layout(G, seed=21)
node_sizes = [3500 * degree_centrality[node] + 300 for node in G.nodes()]
node_colors = [betweenness[node] for node in G.nodes()]
plt.figure(figsize=(10, 7))
nx.draw(
G,
pos,
with_labels=True,
node_size=node_sizes,
node_color=node_colors,
cmap=plt.cm.plasma,
edge_color="#BBBBBB",
font_size=10,
)
plt.title("Influential and Bridging Users in a Social Network")
plt.axis("off")
plt.show()Top 3 by degree centrality: Farah: 0.500 Cara: 0.375 Dina: 0.375 Top 3 by betweenness centrality: Farah: 0.583 Dina: 0.351 Cara: 0.190
- A node can rank highly in degree centrality, betweenness centrality, or both. - High degree often means local influence through many direct connections. - High betweenness often means strategic importance as a bridge between groups. ### Conclusion NetworkX provides a practical toolkit for social network analysis. By combining graph structure, centrality measures, and simple visual encodings, you can quickly identify influential users, bridge nodes, and overall network patterns.