NetworkX Graph

Visualize nodes and edges via Matplotlib.

Try NetworkX Graph Code

How it Works

NetworkX algorithmic maps push interconnected nodes down to coordinates arrays.

The mapping functions link correctly into matplotlib endpoints inside browser memory.

Source Code

Generates a random connected graph tree plotted natively.

graphs.py
Try in Editor
import networkx as nx
import matplotlib.pyplot as plt

# Create a graph
G = nx.erdos_renyi_graph(n=12, p=0.3, seed=42)

# Calculate centrality
centrality = nx.degree_centrality(G)
node_sizes = [v * 3000 for v in centrality.values()]
node_colors = list(centrality.values())

fig, ax = plt.subplots(figsize=(8, 6))
pos = nx.spring_layout(G, seed=42)

# Draw graph
nodes = nx.draw_networkx_nodes(
    G, pos, 
    node_size=node_sizes, 
    node_color=node_colors, 
    cmap=plt.cm.viridis,
    edgecolors='white',
    linewidths=2
)
nx.draw_networkx_edges(G, pos, edge_color='gray', alpha=0.5, width=1.5)
nx.draw_networkx_labels(G, pos, font_color='white', font_family='sans-serif', font_weight='bold')

plt.title("Network Connectedness", fontsize=14, pad=15)
plt.axis('off')
plt.show()
Terminal Output
Rendered Network Graph in the Graphs tab...

Real-world Applications

  • Algorithmic theory
  • Social node rendering
  • Topological queries

Frequently Asked Questions