-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
38 lines (31 loc) · 1.27 KB
/
plot.py
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
import os
import re
import json
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
def savePlot(plt, plotParam, timeSpent):
# Set the path to your directory containing the .png files
path = "./plots"
# Find the largest integer in the existing filenames
existing_ints = []
for filename in os.listdir(path):
match = re.match(r"#(\d+)\.png", filename)
if match:
existing_ints.append(int(match.group(1)))
if existing_ints:
next_int = max(existing_ints) + 1
else:
next_int = 1
title = plotParam['file'] + '\n Network ' + f"#{next_int}" + " \nTime it took: " + str(int(timeSpent)) + " seconds"
plt.title(title)
plt.savefig(os.path.join(path, f"#{next_int}.png"))
def printAndSavePlot(plotParam, timeSpent):
best, = plt.plot(plotParam['generations'], plotParam['allBestFitnesses'], 'g-', label = 'best')
mean, = plt.plot(plotParam['generations'], plotParam['allAvgFitnesses'], 'b-', label = 'mean')
worst, = plt.plot(plotParam['generations'], plotParam['allWorstFitnesses'], 'r-', label = 'worst')
plt.legend([best, mean, worst], ['Best', 'Mean', 'Worst'])
plt.xlabel("Number of generations")
plt.ylabel("Value")
savePlot(plt, plotParam, timeSpent)
plt.show()