Creating curved edges with NetworkX in Python3 (Matplotlib)

Creating curved edges in NetworkX graphs enhances visual clarity when multiple edges exist between nodes. The connectionstyle parameter with arc3 creates smooth curved connections instead of straight lines.

Steps to Create Curved Edges

  • Import required libraries (NetworkX and Matplotlib)
  • Configure figure settings for optimal display
  • Create a directed graph object
  • Add nodes and edges to the graph
  • Use connectionstyle="arc3,rad=0.4" in the draw function
  • Display the graph with curved edges

Example

Here's how to create a directed graph with curved edges ?

import matplotlib.pyplot as plt
import networkx as nx

# Set figure size for better visualization
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create a directed graph
G = nx.DiGraph()

# Add nodes to the graph
G.add_nodes_from([1, 2, 3, 4])

# Add edges between nodes
G.add_edges_from([(1, 2), (2, 4), (2, 3), (4, 1)])

# Create layout for node positioning
pos = nx.spring_layout(G)

# Draw graph with curved edges
nx.draw(G, pos, with_labels=True, connectionstyle="arc3,rad=0.4", 
        node_color='lightblue', node_size=800, font_size=16, 
        arrowsize=20)

plt.show()

The output displays a directed graph with curved edges ?

1 2 3 4

Customizing Curve Radius

You can control the curvature by adjusting the rad parameter ?

import matplotlib.pyplot as plt
import networkx as nx

G = nx.DiGraph()
G.add_nodes_from([1, 2])
G.add_edges_from([(1, 2), (2, 1)])  # Two edges between same nodes

pos = {1: (0, 0), 2: (2, 0)}

# Different curvature values
plt.figure(figsize=(12, 4))

# Subplot 1: Small curve
plt.subplot(1, 3, 1)
nx.draw(G, pos, with_labels=True, connectionstyle="arc3,rad=0.1", 
        node_color='lightgreen', arrowsize=20)
plt.title("rad=0.1 (Small curve)")

# Subplot 2: Medium curve  
plt.subplot(1, 3, 2)
nx.draw(G, pos, with_labels=True, connectionstyle="arc3,rad=0.3",
        node_color='lightcoral', arrowsize=20)
plt.title("rad=0.3 (Medium curve)")

# Subplot 3: Large curve
plt.subplot(1, 3, 3)
nx.draw(G, pos, with_labels=True, connectionstyle="arc3,rad=0.5",
        node_color='lightyellow', arrowsize=20)
plt.title("rad=0.5 (Large curve)")

plt.tight_layout()
plt.show()

Key Parameters

Parameter Description Example Value
rad Curve radius (0 = straight, higher = more curved) 0.1 to 0.5
connectionstyle Edge connection style "arc3,rad=0.4"
arrowsize Size of directional arrows 10-30

Conclusion

Curved edges in NetworkX improve graph readability, especially with multiple connections between nodes. Use connectionstyle="arc3,rad=0.4" for optimal curvature, and adjust the rad parameter to control the curve intensity.

Updated on: 2026-03-25T23:42:15+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements