Area and boundary#

This module provides some basic functionality for obtaining the bounding box and aspect ratio of an embedded graph.

Usage#

To obtain the aspect ratio of an embedded graph simply call aspect_ratio():

>>> g = nx.random_geometric_graph(20, 0.5)
>>> crossing_list = aspect_ratio(g)

To scale the graph into a defined rectangle, call normalize_positions():

>>> pos = normalize_positions(g, box=(0,0,1,1))
>>> nx.set_node_attributes(g, pos)

This preserves the original aspect ratio of the graph. To deform the graph to fit the bounding box perfectly call normalize_positions() with preserve_aspect_ratio = False:

>>> pos = normalize_positions(g, box=(0,0,1,1), preserve_aspect_ratio = False)

Methods#

boundary.area(g: Graph, pos: str | dict | None = None) float#

Calculates the area of the smallest axis-aligned bounding box containing all nodes.

Parameters:
  • g (nx.Graph) – A networkX graph

  • pos (Union[str, dic, None]) – Optional node position dictionary. If not supplied, node positions are read from the graph directly. If given as a string, the property under the given name in the networkX graph is used.

Returns:

Area of the bounding box of g

Return type:

float

boundary.area_tight(g: Graph, pos: str | dict | None = None) float#

Returns the area of the convex hull of the given networkx graph g.

Parameters:
  • g (nx.Graph) – A networkX graph

  • pos (Union[str, dic, None]) – Optional node position dictionary. If not supplied, node positions are read from the graph directly. If given as a string, the property under the given name in the networkX graph is used.

Returns:

Area of the convex hull of g

Return type:

float

boundary.aspect_ratio(g: Graph, pos: str | dict | None = None) float | None#

Calculates the aspect ratio of the given graph. The aspect ratio is defined as the ratio of the bigger side over the smaller side.

Parameters:
  • g (nx.Graph) – A networkX graph

  • pos (Union[str, dic, None]) – Optional node position dictionary. If not supplied, node positions are read from the graph directly. If given as a string, the property under the given name in the networkX graph is used.

Returns:

The aspect ratio of the graph (None in case the graph is empty).

Return type:

Optional[float]

boundary.bounding_box(g: Graph, pos: str | dict | None = None) Tuple[int | float, int | float, int | float, int | float] | None#

Returns the tight bounding box around the given graph.

Parameters:
  • g (nx.Graph) – A networkX graph

  • pos (Union[str, dic, None]) – Optional node position dictionary. If not supplied, node positions are read from the graph directly. If given as a string, the property under the given name in the networkX graph is used.

Returns:

Bounding box in the form (min_x, min_y, max_x, max_y)

Return type:

Optional[Tuple[numeric, numeric, numeric, numeric]]

boundary.height(g: Graph, pos: str | dict | None = None) int | float#

Returns the height of the graph, which is defined as the vertical distance between the lowest and the highest node.

Parameters:
  • g (nx.Graph) – A networkX graph

  • pos (Union[str, dic, None]) – Optional node position dictionary. If not supplied, node positions are read from the graph directly. If given as a string, the property under the given name in the networkX graph is used.

Returns:

The height of the graph

Return type:

numeric

boundary.normalize_positions(g: Graph, pos: str | dict | None = None, box: Tuple[int | float, int | float, int | float, int | float] = (-0.5, -0.5, 0.5, 0.5), preserve_aspect_ratio: bool = True) dict#

Normalizes the positions of the graph to fit within a given bounding box.

Parameters:
  • g (nx.Graph) – A networkX graph

  • pos (Union[str, dic, None]) – Optional node position dictionary. If not supplied, node positions are read from the graph directly. If given as a string, the property under the given name in the networkX graph is used.

  • box (Tuple[numeric, numeric, numeric, numeric]) – The bounding box b = (min_x, min_y, max_x, max_y) to fit the graph into. By default, the bounding box is equal to (-0.5, -0.5, 0.5, 0.5)

  • preserve_aspect_ratio (bool) – Whether or not to preserve the aspect ratio. If false, the graph is distorted to fill the bounding box exactly.

Returns:

The new node positions

Return type:

dict

boundary.width(g: Graph, pos: str | dict | None = None) int | float#

Returns the width of the graph, which is defined as the horizontal distance between the left-most and the right-most node.

Parameters:
  • g (nx.Graph) – A networkX graph

  • pos (Union[str, dic, None]) – Optional node position dictionary. If not supplied, node positions are read from the graph directly. If given as a string, the property under the given name in the networkX graph is used.

Returns:

The width of the graph

Return type:

numeric