grafx.draw

draw_grafx(G, vertical=False, compute_node_position_fn=<function compute_node_position>, draw_node_fn=<function draw_node>, draw_edge_fn=<function draw_edge>, colors=None, **kwargs)

Draw an input GRAFX object. It first computes the node positions, then draws each node and edge. These functions, compute_node_position(), draw_node(), and draw_edge(), respectively, can be customized by passing custom functions or modules to the arguments.

Parameters:
  • G (GRAFX) – An input graph to draw.

  • vertical (bool, optional) – If True, the nodes are organized from top to bottom. If False, the nodes are organized from left to right (default: False).

  • compute_node_position_fn (Callable, optional) – Any function or module that computes and stores node positions into G as a key x0 and y0. (default: compute_node_position()).

  • draw_node_fn (Callable(), optional) – Any function or module that draws each node of G (default: draw_node()).

  • draw_edge_fn (Callable, optional) – Any function or module that draws each edge of G (default: draw_edge()).

  • colors (List, Dict, or None, optional) – Collection of face colors for each node type. If a List is given, each type’s color will be assigned based on its initial letter. If a Dict is given, the color will be exactly assigned based on that dictionary. None will use the default color scheme. All of these are handled with NodeColorHandler (default: None).

  • **kwargs (optional) – All additional keyword arguments passed to compute_node_position_fn, draw_node_fn, and draw_edge_fn. Each keyword must start with "position_", "node_", or "edge_" so that it can passed to one of the three appropriately. For example, “node_size” will be passed to draw_node_fn as a key "size".

Returns:

A matplotlib plot that the input graph is visualized on.

Return type:

Tuple[Figure, Axes]

compute_node_position(G, node_spacing=(0.8, 0.8))

Calculates and assigns \(x\) and \(y\) coordinates to the nodes in the graph based on their ranks and relative positions.

Parameters:
  • G (GRAFX) – The graph whose node positions are to be computed.

  • node_spacing (Tuple[float], optional) – The horizontal and vertical distance between nodes (default: (0.8, 0.8)).

Returns:

None

draw_node(ax, G, node, color_config, vertical=False, inside='node_type', above=None, size=(0.5, 0.5), linewidth=0.6, inside_fontsize=5.6, above_fontsize=3.0)

Draws a node as an rectangle on the provided axes.

Parameters:
  • ax (matplotlib.pyplot.Axes) – Pre-existing axes for the plot.

  • G (GRAFX) – A full graph that will be drawn.

  • node (Tuple[int, dict]) – Node attributes of the target node to be drawn. It is an individual item of the list returned by GRAFX.nodes(data=True). It must contain inside and above

  • color_config (List or None, optional) – (default: None).

  • inside (str, optional) – Key of a node attribute that will be displayed inside the node G as a key x0 and y0. (default: "node_type").

  • above (str or None, optional) – Key of a node attribute that will be displayed above the node Any function or module that draws each node of G (default: None).

  • size (Tuple[float], optional) – A size of node shown as a rectangle (default: draw_edge).

  • linewidth (float, optional) – Thickness of the border of the rectangle (default: draw_edge).

  • inside_fontsize (float, optional) – Size of the text inside of the rectangle (default: 5.6).

  • above_fontsize (float, optional) – Size of the text above of the rectangle (default: 3.0).

Returns:

None

draw_edge(ax, G, edge, vertical, linewidth=0.6)

Draw an edge between two nodes in a graph.

Parameters:
  • ax (matplotlib.axes.Axes) – Pre-existing axes for the plot.

  • G (GRAFX) – A full graph that will be drawn.

  • edge (Tuple[int, int, dict]) – A tuple representing the edge, containing the source ID, destination ID, and edge data.

  • linewidth (float, optional) – The line width of the edge (default: 0.6).

Returns:

None

class NodeColorHandler(facecolor_map=None, node_types=None, colors=None)

A class that handles the color mapping for each node type in the graph.

Parameters:
  • facecolor_map (Dict[str, COLORTYPE] or None, optional) – A dictionary that maps each node type to a face color. If given, the other arguments are ignored. If None, the other arguments must be provided, as they are used to generate the mapping (default: None).

  • node_types (List[str] or None, optional) – A list of node types that can be used in the graph (default: None).

  • colors (List[COLORTYPE] or None, optional) – A list of colors (in any format that matplotlib recognizes). This module will assign each node type a color based on its initial letter, and if this fails due to the lack of colors, it will assign a random color. (default: None).

get_colors(node_type)

Retrieves the face and edge color for the given node type.

Parameters:

node_type (str) – The type of the node.

Returns:

A dictionary with the face and edge color.

Return type:

Dict[str, COLORTYPE]