Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Colouring the edges by weight in networkx (Matplotlib)
In NetworkX with Matplotlib, you can color graph edges based on their weights to create visually informative network visualizations. This technique helps highlight important connections and patterns in your network data.
Basic Edge Coloring by Weight
Here's how to create a directed graph with weighted edges and color them accordingly minus;
import random as rd
import matplotlib.pyplot as plt
import networkx as nx
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create directed graph
G = nx.DiGraph()
G.add_nodes_from([1, 2, 3, 4])
G.add_edges_from([(1, 2), (2, 3), (3, 4), (4, 1), (1, 3)])
# Assign random weights to edges
for u, v, d in G.edges(data=True):
d['weight'] = rd.random()
# Extract edges and weights for coloring
edges, weights = zip(*nx.get_edge_attributes(G, 'weight').items())
# Draw graph with colored edges
nx.draw(G, node_color='b', edge_color=weights, width=2, with_labels=True)
plt.show()
Advanced Edge Coloring with Colormap
For better control over colors, you can specify a colormap and add a colorbar minus;
import random as rd
import matplotlib.pyplot as plt
import networkx as nx
# Create graph with weighted edges
G = nx.Graph()
G.add_edges_from([
(1, 2, {'weight': 0.8}),
(2, 3, {'weight': 0.3}),
(3, 4, {'weight': 0.9}),
(4, 1, {'weight': 0.1}),
(1, 3, {'weight': 0.6})
])
# Get edge weights
weights = [G[u][v]['weight'] for u, v in G.edges()]
# Create layout
pos = nx.spring_layout(G, seed=42)
# Draw with custom colormap
plt.figure(figsize=(8, 6))
edges = nx.draw_networkx_edges(G, pos, edge_color=weights,
edge_cmap=plt.cm.viridis, width=3)
nx.draw_networkx_nodes(G, pos, node_color='lightblue', node_size=500)
nx.draw_networkx_labels(G, pos)
# Add colorbar
plt.colorbar(edges, label='Edge Weight')
plt.title('Graph with Edge Colors Based on Weights')
plt.axis('off')
plt.show()
Displaying Edge Weights as Labels
You can also display the actual weight values on the edges minus;
import matplotlib.pyplot as plt
import networkx as nx
# Create weighted graph
G = nx.Graph()
edges_with_weights = [
(1, 2, 0.7),
(2, 3, 0.4),
(3, 4, 0.9),
(4, 1, 0.2)
]
for u, v, w in edges_with_weights:
G.add_edge(u, v, weight=w)
# Get weights and create position layout
weights = [G[u][v]['weight'] for u, v in G.edges()]
pos = nx.circular_layout(G)
# Draw graph
plt.figure(figsize=(8, 6))
nx.draw_networkx_nodes(G, pos, node_color='lightcoral', node_size=600)
nx.draw_networkx_edges(G, pos, edge_color=weights, edge_cmap=plt.cm.plasma, width=4)
nx.draw_networkx_labels(G, pos, font_size=16, font_weight='bold')
# Draw edge labels with weights
edge_labels = nx.get_edge_attributes(G, 'weight')
nx.draw_networkx_edge_labels(G, pos, edge_labels, font_size=12)
plt.title('Network with Edge Weights and Colors')
plt.axis('off')
plt.show()
Key Parameters
| Parameter | Description | Example Values |
|---|---|---|
edge_color |
Edge colors based on weights | List of weight values |
edge_cmap |
Colormap for edge coloring |
plt.cm.viridis, plt.cm.plasma
|
width |
Edge thickness | 1, 2, 3 (integer values) |
Conclusion
Coloring edges by weight in NetworkX creates informative visualizations where edge colors represent relationship strength. Use edge_color with weight values and specify a colormap for better visual distinction between different weight ranges.
