graphinate.renderers.matplotlib   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 38
dl 0
loc 83
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A plot() 0 26 1
B draw() 0 46 5
1
from typing import Any
2
3
import networkx as nx
4
from matplotlib import pyplot
5
6
from ..color import node_color_mapping
7
8
9
def draw(graph: nx.Graph,
10
         with_node_labels: bool = True,
11
         with_edge_labels: bool = False,
12
         **kwargs: Any) -> None:
13
    """
14
    Draws the given networkx graph with optional node and edge labels.
15
16
    Args:
17
        graph (nx.Graph): The input graph to be drawn.
18
        with_node_labels (bool): Whether to display node labels. Default is True.
19
        with_edge_labels (bool): Whether to display edge labels. Default is False.
20
21
    Returns:
22
        None
23
    """
24
    pos = nx.planar_layout(graph) if nx.is_planar(graph) else None
25
    pos = nx.spring_layout(graph, pos=pos) if pos else nx.spring_layout(graph)
26
27
    draw_params = {}
28
    if with_node_labels:
29
        draw_params.update(
30
            {
31
                'with_labels': True,
32
                'labels': nx.get_node_attributes(graph, 'label'),
33
                'font_size': 6,
34
                'font_color': 'blue',
35
                # 'horizontalalignment':'left',
36
                # 'verticalalignment':'bottom',
37
                # 'bbox': {
38
                #     'boxstyle': 'round',
39
                #     'fc': (0.02, 0.02, 0.02),
40
                #     'lw': 0,
41
                #     'alpha': 0.15,
42
                #     'path_effects': [patheffects.withStroke(linewidth=1, foreground="red")]
43
                # }
44
            }
45
        )
46
47
    node_color = list(node_color_mapping(graph).values())
48
    nx.draw(graph, pos, node_color=node_color, **draw_params)
49
    if with_edge_labels:
50
        nx.draw_networkx_edge_labels(graph,
51
                                     pos,
52
                                     edge_labels=nx.get_edge_attributes(graph, 'label'),
53
                                     font_color='red',
54
                                     font_size=6)
55
56
57
def plot(graph: nx.Graph,
58
         with_node_labels: bool = True,
59
         with_edge_labels: bool = False,
60
         **kwargs: Any) -> None:
61
    """
62
    Plots the given networkx graph with optional node and edge labels.
63
64
    Args:
65
        graph (nx.Graph): The input graph to be plotted.
66
        with_node_labels (bool): Whether to display node labels. Default is True.
67
        with_edge_labels (bool): Whether to display edge labels. Default is False.
68
69
    Returns:
70
        None
71
    """
72
    draw(graph, with_node_labels, with_edge_labels, **kwargs)
73
74
    ax = pyplot.gca()
75
    ax.margins(0.10)
76
77
    fig = pyplot.gcf()
78
    fig.suptitle(graph.name)
79
    fig.tight_layout()
80
81
    # pyplot.axis("off")
82
    pyplot.show()
83