ForceAtlas2 for Python¶
The fastest Python implementation of the ForceAtlas2 graph layout algorithm, with Cython optimization for 10-100x speedup.
500-node stochastic block model (7 communities) laid out with ForceAtlas2 LinLog mode
1000-node stochastic block model (8 communities) laid out in 3D with ForceAtlas2 LinLog mode
Key Features¶
- Fast: Cython extension provides 10-100x speedup over pure Python
- Scalable: Barnes-Hut approximation for O(n log n) complexity
- N-dimensional: 2D, 3D, and higher-dimensional layouts via
dimparameter - Anti-collision:
adjustSizesprevents node overlap (Gephi parity) - Auto-tuning:
inferSettings()selects parameters based on graph characteristics - Multiple backends: Cython, NumPy vectorized, or pure Python fallback
- Flexible input: NetworkX, igraph, NumPy arrays, or SciPy sparse matrices
- Reproducible: Seeded layouts via
seedparameter - Observable: Callbacks for animation and iteration monitoring
Quick Example¶
from fa2.easy import layout, visualize
# No numpy required — just edge lists
positions = layout([("A", "B"), ("B", "C"), ("A", "C")], mode="community")
# Or render directly
visualize([("A", "B"), ("B", "C")], output="png", path="graph.png")
Or with the full API:
import networkx as nx
from fa2 import ForceAtlas2
G = nx.karate_club_graph()
fa2 = ForceAtlas2(linLogMode=True, verbose=False)
positions = fa2.forceatlas2_networkx_layout(G, iterations=100)
What's New in v1.1.0¶
- Simple API (
fa2.easy) —layout()andvisualize(), no numpy required - CLI —
python -m fa2 layout/render/metricswith JSON/CSV input - Visualization (
fa2.viz) — matplotlib rendering, JSON/PNG/SVG/GEXF export - Quality metrics (
fa2.metrics) — stress, edge crossings, neighborhood preservation - MCP server — AI agents can use ForceAtlas2 as a tool
- Mode presets — "community", "hub-dissuade", "compact"
What's New in v1.0.0¶
- N-dimensional layout (
dimparameter) — 2D, 3D, and beyond adjustSizes— anti-collision forces prevent node overlapinferSettings()— auto-tuning based on graph characteristicsnormalizeEdgeWeightsandinvertedEdgeWeightsMode- NumPy vectorized backend — 10-16x faster than pure Python loops
store_pos_as— save positions as node attributessize_attr— read node sizes from graph attributes