Skip to content

ForceAtlas2 for Python

CI PyPI version Python 3.9+ License: GPL v3

The fastest Python implementation of the ForceAtlas2 graph layout algorithm, with Cython optimization for 10-100x speedup.

ForceAtlas2 layout animation

500-node stochastic block model (7 communities) laid out with ForceAtlas2 LinLog mode

ForceAtlas2 3D layout animation — 1000 nodes with 8 communities

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 dim parameter
  • Anti-collision: adjustSizes prevents 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 seed parameter
  • 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() and visualize(), no numpy required
  • CLIpython -m fa2 layout/render/metrics with 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 (dim parameter) — 2D, 3D, and beyond
  • adjustSizes — anti-collision forces prevent node overlap
  • inferSettings() — auto-tuning based on graph characteristics
  • normalizeEdgeWeights and invertedEdgeWeightsMode
  • NumPy vectorized backend — 10-16x faster than pure Python loops
  • store_pos_as — save positions as node attributes
  • size_attr — read node sizes from graph attributes

Get Started API Reference