-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualization.py
More file actions
35 lines (29 loc) · 1.34 KB
/
Copy pathVisualization.py
File metadata and controls
35 lines (29 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
from string import ascii_uppercase as letters
from Graph import *
class Visualization:
# letters is easier to look at than numbers so table will look like - {0: 'A', 1: 'B', ..., 25: 'C'}
table: dict[int, str] = {idx: letter for idx, letter in enumerate(letters)}
@staticmethod
def visualize_graph(graph: Graph, **optional) -> None:
nx_graph = nx.Graph(np.array(graph.get_matrix()))
labels = None
if graph.get_number() <= 25 and optional.get('labels') == True:
labels = {k: v for k, v in Visualization.table.items() if k < graph.get_number()}
nx.draw(nx_graph, with_labels=True, labels=labels)
plt.show()
def visualize_matrix(matrix: list[list[int]], **optional) -> None:
# printing without labels (A, B, C, etc) if we don't have enough letters or we don't want to
if (len(matrix) > 26 or optional.get('labels') == False):
for row in matrix:
print(' '.join(map(str, row)))
return
# printing with labels
print(' ', end=' ')
for el in letters[0:len(matrix)]:
print(el, end=' ')
print()
for idx, row in enumerate(matrix):
print(letters[idx] + ' ' + ' '.join(map(str, row)))