NetworkX Graph
Visualize nodes and edges via Matplotlib.
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.
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()Rendered Network Graph in the Graphs tab...Real-world Applications
- Algorithmic theory
- Social node rendering
- Topological queries
Frequently Asked Questions
More Examples
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Sort a List in Python
Learn how to sort a list in Python using the sort() method and the sorted() function. Discover custom key sorting and reverse order examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.