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
How do I customize the display of edge labels using networkx in Matplotlib?
Customizing edge labels in NetworkX with Matplotlib allows you to control the appearance and positioning of text displayed along graph edges. You can adjust label position, font properties, and styling to create clear, professional network visualizations.
Basic Edge Label Display
First, let's create a simple graph with edge labels ?
import matplotlib.pyplot as plt
import networkx as nx
# Create a 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)])
# Position nodes using spring layout
pos = nx.spring_layout(G, seed=42)
# Draw the graph
nx.draw(G, pos, node_color='lightblue', edge_color='gray',
with_labels=True, node_size=1000, font_size=16)
# Add basic edge labels
edge_labels = {(1, 2): "A", (2, 3): "B", (3, 4): "C", (4, 1): "D", (1, 3): "E"}
nx.draw_networkx_edge_labels(G, pos, edge_labels)
plt.title("Basic Edge Labels")
plt.axis('off')
plt.tight_layout()
plt.show()
Customizing Label Position
Use the label_pos parameter to control where labels appear along edges ?
import matplotlib.pyplot as plt
import networkx as nx
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)])
pos = nx.spring_layout(G, seed=42)
# Draw the graph
nx.draw(G, pos, node_color='lightblue', edge_color='gray',
with_labels=True, node_size=1000, font_size=16)
# Edge labels with custom positioning
edge_labels = {(1, 2): "Near End", (2, 3): "Center", (3, 4): "Near Start",
(4, 1): "Custom", (1, 3): "Default"}
# label_pos: 0.0 = start of edge, 0.5 = middle, 1.0 = end of edge
nx.draw_networkx_edge_labels(G, pos, edge_labels, label_pos=0.75)
plt.title("Edge Labels at 75% Position")
plt.axis('off')
plt.tight_layout()
plt.show()
Styling Edge Labels
Customize font properties, colors, and appearance ?
import matplotlib.pyplot as plt
import networkx as nx
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)])
pos = nx.spring_layout(G, seed=42)
# Draw the graph
nx.draw(G, pos, node_color='lightblue', edge_color='gray',
with_labels=True, node_size=1000, font_size=16)
# Styled edge labels
edge_labels = {(1, 2): "Bold", (2, 3): "Red", (3, 4): "Large",
(4, 1): "Green", (1, 3): "Style"}
nx.draw_networkx_edge_labels(G, pos, edge_labels,
font_size=12,
font_color='red',
font_weight='bold',
font_family='serif',
label_pos=0.6)
plt.title("Styled Edge Labels")
plt.axis('off')
plt.tight_layout()
plt.show()
Edge Labels with Weights
Display numerical weights or distances on edges ?
import matplotlib.pyplot as plt
import networkx as nx
# Create weighted graph
G = nx.Graph()
G.add_weighted_edges_from([(1, 2, 5.2), (2, 3, 8.7), (3, 4, 2.1),
(4, 1, 6.8), (1, 3, 3.5)])
pos = nx.spring_layout(G, seed=42)
# Draw the graph
nx.draw(G, pos, node_color='lightgreen', edge_color='darkblue',
with_labels=True, node_size=1200, font_size=16)
# Extract edge weights for labels
edge_labels = nx.get_edge_attributes(G, 'weight')
# Format weights to 1 decimal place
edge_labels = {edge: f"{weight:.1f}" for edge, weight in edge_labels.items()}
nx.draw_networkx_edge_labels(G, pos, edge_labels,
font_size=10,
font_color='darkred',
bbox=dict(boxstyle='round,pad=0.2',
facecolor='white',
edgecolor='gray'))
plt.title("Edge Labels with Weights")
plt.axis('off')
plt.tight_layout()
plt.show()
Label Customization Options
| Parameter | Description | Example Values |
|---|---|---|
label_pos |
Position along edge (0.0 to 1.0) | 0.0 (start), 0.5 (middle), 1.0 (end) |
font_size |
Text size | 8, 10, 12, 16 |
font_color |
Text color | 'red', 'blue', '#FF0000' |
font_weight |
Text boldness | 'normal', 'bold' |
bbox |
Background box styling | dict with boxstyle, facecolor |
Conclusion
NetworkX provides flexible options for customizing edge labels through draw_networkx_edge_labels(). Use label_pos to control positioning and font parameters for styling. Add background boxes with bbox for better readability in complex networks.
