plot_graph¶

plot_graph(canvas, nodes, edges, node_colors=(255, 0, 0), node_size=5, edge_colors=(0, 0, 0), edge_size=5)[source]¶

Drawing a graph onto a canvas.

Drawing a graph onto a canvas.

Parameters:
  • canvas (np.ndarray) – Canvas to be drawn upon.

  • nodes (np.ndarray) – List of nodes, expected to be Nx2 where N is the number of nodes. Each node is expected to be of (x, y) and should be within the height and width of the canvas.

  • edges (np.ndarray) – List of edges, expected to be Mx2 where M is the number of edges. Each edge is defined as a pair of indexes (from, to), where each corresponds to a node of within nodes.

  • node_colors (tuple or np.ndarray) – A color or list of node colors. Each color is expected to be (r, g, b) and is between 0 and 255.

  • edge_colors (tuple or np.ndarray) – A color or list of node colors. Each color is expected to be (r, g, b) and is between 0 and 255.

  • node_size (int) – Radius of each node.

  • edge_size (int) – Line width of the edge.

Return type:

ndarray

Examples

>>> from tiatoolbox.utils.visualization import plot_graph
>>> import numpy as np
>>> # Generate a random example; replace with your own data
>>> canvas = np.zeros((256, 256, 3), dtype=np.uint8)
>>> num_nodes = 10
>>> nodes = np.random.randint(0, 255, size=(num_nodes, 2))
>>> num_edges = 15
>>> edges = np.random.randint(0, num_nodes, size=(num_edges, 2))
>>> node_colors = np.random.randint(0, 256, size=(num_nodes, 3))
>>> edge_colors = np.random.randint(0, 256, size=(num_edges, 3))
>>> # Example usage of overlay_prediction_contours
>>> overlaid_canvas = plot_graph(
...     canvas=canvas,
...     nodes=nodes,
...     edges=edges,
...     node_colors=node_colors,
...     node_size=8,
...     edge_colors=edge_colors,
...     edge_size=3
... )