def circular_layout(G, scale=1, center=None, dim=2, direction='CCW'):
# dim=2 only
"""Position nodes on a circle.
Parameters
----------
G : NetworkX graph or list of nodes
scale : float
Scale factor for positions
center : array-like or None
Coordinate pair around which to center the layout.
dim : int
Dimension of layout, currently only dim=2 is supported
Returns
-------
pos : dict
A dictionary of positions keyed by node
Examples
--------
>>> G = nx.path_graph(4)
>>> pos = nx.circular_layout(G)
Notes
-----
This algorithm currently only works in two dimensions and does not
try to minimize edge crossings.
"""
G, center = _process_params(G, center, dim)
if len(G) == 0:
pos = {}
elif len(G) == 1:
pos = {nx.utils.arbitrary_element(G): center}
else:
# Discard the extra angle since it matches 0 radians.
theta = np.linspace(0, 1, len(G) + 1)[:-1] * 2 * np.pi
theta = theta.astype(np.float32)
if direction == 'CCW':
pos = np.column_stack([np.cos(theta), np.sin(theta)])
else:
pos = np.column_stack([np.sin(theta), np.cos(theta)])
pos = rescale_layout(pos, scale=scale) + center
pos = dict(zip(G, pos))
return pos
评论列表
文章目录