PyForceAtlas2
PyForceAtlas2 is a Python implementation of the ForceAtlas2 graph layout algorithm. Originally designed for Gephi, this implementation is optimized for reproducible and high-performance network visualization in Python, supporting both NetworkX and igraph.
Features
- High Performance: Barnes-Hut optimization for large networks.
- Customizable: Fine-tune attraction, repulsion, gravity, and speed parameters.
- Versatile: Seamless integration with NetworkX and igraph.
- Reproducible: Easily set random seeds for deterministic layouts.
Installation
Install PyForceAtlas2 via pip:
pip install pyforceatlas2
Usage Example
The following example demonstrates how to compute a layout for a random geometric graph and visualize it with NetworkX and Matplotlib:
"""
Example: ForceAtlas2 Layout on a Random Geometric Graph
--------------------------------------------------------
This script generates a random geometric graph with 400 nodes,
computes a ForceAtlas2 layout, and visualizes the result.
"""
import random
import networkx as nx
import matplotlib.pyplot as plt
from pyforceatlas2 import ForceAtlas2
# Set a random seed for reproducibility.
random.seed(888)
# Generate a random geometric graph: nodes are connected if they are within radius 0.2.
G = nx.random_geometric_graph(400, 0.2)
# Initialize ForceAtlas2 with custom parameters.
forceatlas2 = ForceAtlas2(
outbound_attraction_distribution=False, # Do not dissuade hubs.
edge_weight_influence=1.0,
lin_log_mode=True, # Use LinLog mode for attraction forces.
jitter_tolerance=10.0, # Tolerance for oscillations.
barnes_hut_optimize=True, # Enable Barnes-Hut optimization.
barnes_hut_theta=2.2, # Trade-off between accuracy and speed.
scaling_ratio=2.0, # Controls overall repulsion strength.
strong_gravity_mode=False, # Use standard gravity.
gravity=1.0, # Gravitational constant.
verbose=True,
)
# Compute the layout. Returns a dict mapping each node to an (x, y) coordinate.
positions = forceatlas2.forceatlas2_networkx_layout(G, pos=None, iterations=100)
# Visualize the graph.
nx.draw_networkx_nodes(G, positions, node_size=20, node_color="blue", alpha=0.4)
nx.draw_networkx_edges(G, positions, edge_color="green", alpha=0.05)
plt.axis("off")
plt.show()
API Overview
The main entry point is the ForceAtlas2 class:
from pyforceatlas2 import ForceAtlas2
# Initialize with desired parameters.
forceatlas2 = ForceAtlas2(
outbound_attraction_distribution=False,
edge_weight_influence=1.0,
lin_log_mode=True,
jitter_tolerance=10.0,
barnes_hut_optimize=True,
barnes_hut_theta=2.2,
scaling_ratio=2.0,
strong_gravity_mode=False,
gravity=1.0,
verbose=True,
)
# For a NetworkX graph:
positions = forceatlas2.forceatlas2_networkx_layout(G, pos=None, iterations=100)
# For an igraph graph:
layout_obj = forceatlas2.forceatlas2_igraph_layout(igraph_graph, pos=None, iterations=100)
References
- ForceAtlas2 Paper:
Jacomy, M., Venturini, T., Heymann, S., & Bastian, M. (2014).
ForceAtlas2, a continuous graph layout algorithm for handy network visualization designed for the Gephi software.
PLoS ONE, 9(6), e98679.
DOI: 10.1371/journal.pone.0098679
Contributing
Contributions, bug reports, and feature requests are welcome! Please open an issue or submit a pull request on GitHub.
License
This project is licensed under the GNU General Public License v3.0 (GPLv3). See the LICENSE file for details.
Metadata
Release files for pyforceatlas2 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pyforceatlas2-0.0.5.tar.gz | 30.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyforceatlas2-0.0.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 56.5 kB
Release files / pyforceatlas2-0.0.5.tar.gz
| Download URL | pyforceatlas2-0.0.5.tar.gz |
|---|---|
| Size | 30.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
814a0aefd4805077b0821b8a83ee34e7e6adfcef549e3379b49f58528f8d939a
|
|
BLAKE2b-256 checksum How to use checksums |
924b01c20b853b6ea35fa05a637dc4e709d915801af6d96018f303d6d51e9cc4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Release files / pyforceatlas2-0.0.5-py3-none-any.whl
| Download URL | pyforceatlas2-0.0.5-py3-none-any.whl |
|---|---|
| Size | 26.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c33b0cae5b5ab3b2ae671ca2b24fd410316bf5b631e050d30826b6ab65b70c9a
|
|
BLAKE2b-256 checksum How to use checksums |
4f874b2cc9dac91fc845e24c51051a9b86c4b6a8eaa690ced538ec90945e26fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Security for pyforceatlas2 0.0.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.