-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
76 lines (56 loc) · 2.73 KB
/
plot.py
File metadata and controls
76 lines (56 loc) · 2.73 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import matplotlib.pyplot as plt
import numpy as np
import cmcrameri.cm as cmc
# Set global font size
plt.rcParams.update({'font.size': 16})
color = cmc.batlowS
def convert_strings_to_floats(string_array) -> list[float]:
float_array = [float(value) for value in string_array]
return float_array
def div_array(array1: list, array2: list, factor: int | float = 1) -> list[float]:
array1 = convert_strings_to_floats(array1)
array2 = convert_strings_to_floats(array2)
result_array = [a / b / factor for a, b in zip(array1, array2)]
return result_array
def normalize(instances: dict[str, list[float | str]]) -> dict[str, list[float]]:
max_list = [max(convert_strings_to_floats(values)) for values in zip(*instances.values())] # print(result_list)
for instance_name in instances.keys():
instances[instance_name] = div_array(instances[instance_name], max_list)
return instances
def plot(categories: list[str], instances: dict[str, list[str | float | int]], xlabel='', ylabel='', title='',
filename='test', suffix='', rotation_x=0, extra_with=False):
bar_width = 0.35
bar_positions = np.arange(len(categories)) * len(instances) / 2.5
if extra_with:
plt.figure(figsize=(16, 6)) # Width: 8 inches, Height: 6 inches
else:
plt.figure(figsize=(8, 6)) # Width: 8 inches, Height: 6 inches
for i, instance in enumerate(sorted(instances.keys())):
# Create grouped bar chart
plt.bar(bar_positions, convert_strings_to_floats(instances[instance]),
width=bar_width, label=instance, color=color(i))
bar_positions = bar_positions + bar_width
# Add labels and title
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
# Add a legend
if extra_with:
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1), fancybox=True, shadow=True, ncol=5)
else: # Set x-axis ticks and labels
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1), fancybox=True, shadow=True, ncol=2)
plt.xticks(bar_positions - bar_width * (len(instances) + 1) / 2, categories, rotation=rotation_x)
# Save the figure to an SVG file
plt.savefig(filename + suffix + '.svg', format='svg', bbox_inches='tight')
# Display the plot (optional)
plt.show()
def to_float_array(string_array: list[str]) -> list[float]:
return [float(x) for x in string_array]
def average(float_array: list[float]) -> float:
return round(sum(float_array) / len(float_array), 2)
def filter_dictionary(input_dict, filter_keys):
if not filter_keys:
return input_dict
# Using dictionary comprehension to filter keys
filtered_dict = {key: value for key, value in input_dict.items() if key in filter_keys}
return filtered_dict