Skip to content

Commit

Permalink
feat: enforce equal aspect for circular layout #430
Browse files Browse the repository at this point in the history
  • Loading branch information
maximelucas committed Jul 26, 2023
1 parent 059b9c9 commit 18eec96
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions xgi/drawing/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ def draw(
# compute axis limits
_update_lims(pos, ax)

if _pos_is_circular(pos):
ax.set_aspect("equal")

return ax


Expand Down Expand Up @@ -355,6 +358,9 @@ def draw_nodes(
# compute axis limits
_update_lims(pos, ax)

if _pos_is_circular(pos):
ax.set_aspect("equal")

return ax


Expand Down Expand Up @@ -1151,6 +1157,36 @@ def _update_lims(pos, ax):
ax.autoscale_view()


def _pos_is_circular(pos):
"""
Returns True if positions in `pos` correspond to the circular layout.
Parameters
----------
pos : dict
A dictionary containing the positions of nodes. The keys are node identifiers,
and the values are 2D coordinate tuples or arrays.
Returns
-------
bool
"""

N = len(pos)
pos_arr = np.array(list(pos.values()))

radii = np.linalg.norm(pos_arr, axis=1)
angles = np.arctan2(pos_arr[:,1], pos_arr[:,0])
angles_diff = np.diff(angles) % (2*np.pi)

same_radii = np.allclose(radii, radii[0])
same_angles_diff = np.allclose(angles_diff, angles_diff[0])
correct_angle = np.allclose(angles_diff, 2 * np.pi / N)

return (same_radii and same_angles_diff and correct_angle)


def _draw_hull(node_pos, ax, edges_ec, facecolor, alpha, zorder, radius):
"""Draw a convex hull encompassing the nodes in node_pos
Expand Down

0 comments on commit 18eec96

Please sign in to comment.