Skip to content

Instantly share code, notes, and snippets.

@cvanelteren
Created October 19, 2023 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvanelteren/6dedb7f3c1cad3027dc2cff3e25db23d to your computer and use it in GitHub Desktop.
Save cvanelteren/6dedb7f3c1cad3027dc2cff3e25db23d to your computer and use it in GitHub Desktop.
Networkx simple hive plot
def arc_layout(
G: nx.Graph, subset_key="subset", radius=1, rotation=0, offset=0
) -> dict:
"""Arc layout for networkx
Provides a layout where a multipartite graph is
displayed on a unit circle. This could provide clear
visuals for data that is highly clustered.
Parameters
----------
G : nx.Graph
Networkx (Di)Graph
subset_key : object
Node attribute to cluster the network on
radius : float
Radius of the unit circle
rotation : float
Rotation of the axes of the unit circle
offset : float
Offset for the first node in the spoke
Returns
-------
pos : dict
A dictionary of positions keyed by node.
Examples
--------
>>> subset_sizes = [5, 5, 4, 3, 2, 4, 4, 3]
>>> G = nx.complete_multipartite_graph(*subset_sizes)
>>> pos = nx.arc_layout(G, subset_key = "subset")
"""
import numpy as np
attrs = nx.get_node_attributes(G, subset_key)
categories = set(attrs.values())
angles = np.linspace(0, 2 * np.pi, len(categories), 0) + rotation
pos = {}
for category, angle in zip(categories, angles):
# collect nodes
subset = [node for node, attr in attrs.items() if attr == category]
radii = np.linspace(offset, radius, len(subset), 0)
for node, rad in zip(subset, radii):
pos[node] = rad * np.array([np.cos(angle), np.sin(angle)])
return pos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment