From efb69f88eaaa2462d26da4d164019705a85b1f62 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Thu, 12 Nov 2020 21:16:17 -0500 Subject: [PATCH] Updated plt usage --- src/database/matplotlib_graph.py | 76 ++++++++------------------------ src/run_testing.py | 10 ++++- 2 files changed, 27 insertions(+), 59 deletions(-) diff --git a/src/database/matplotlib_graph.py b/src/database/matplotlib_graph.py index 43df5dd..608462c 100644 --- a/src/database/matplotlib_graph.py +++ b/src/database/matplotlib_graph.py @@ -12,44 +12,7 @@ class Matplotlib_Graph: def __init__(self, database): self.database = database - # Type - self.type_of_plot = plt.plot - # Size - self.size = [6,6] - # Labels - self.xlabel = None - self.ylabel = None - self.title = None - # Scale - self.yscale = "linear" - # Data points - self.x = None - self.y = None - - - def plot(self): - """Plot all the graph attributes""" - - if self.yscale == "log": - # If using log then the values have to be positive numbers - self.y = [abs(ele) for ele in self.y] - - # Setup data - plt.figure(figsize = self.size) - plt.yscale(self.yscale) - self.type_of_plot(self.x, self.y) - - # labels - #if self.xlabel is not None: xlabel = self.xlabel - #if self.ylabel is not None: xlabel = self.ylabel - #if self.title is not None: xlabel = self.title - - plt.xlabel(self.xlabel) - plt.ylabel(self.ylabel) - plt.title(self.title) - - # Show the plot - plt.show() + self.type_of_plot = 'line' def generation_total_fitness(self): @@ -59,16 +22,15 @@ class Matplotlib_Graph: generations = self.database.get_total_generations() # Create the generations list - [0,1,2,etc] - self.x = list(range(0, generations)) + X = list(range(0, generations)) # Query for Y data - self.y = self.database.get_generation_total_fitness() + Y = self.database.get_generation_total_fitness() - self.xlabel = 'Generation' - self.ylabel = 'Generation Total Fitness' - self.title = 'Relationship Between Generations and Generation Total Fitness' - - self.plot() + self.type_of_plot(X, Y) + plt.xlabel('Generation') + plt.ylabel('Generation Total Fitness') + plt.title('Relationship Between Generations and Generation Total Fitness') def highest_value_chromosome(self): @@ -78,16 +40,16 @@ class Matplotlib_Graph: generations = self.database.get_total_generations() # Create the generations list - [0,1,2,etc] - self.x = list(range(0, generations)) + X = list(range(0, generations)) # Query for Y data - self.y = self.database.get_highest_chromosome() + Y = self.database.get_highest_chromosome() - self.xlabel = 'Generation' - self.ylabel = 'Highest Fitness' - self.title = 'Relationship Between Generations and Highest Fitness' + self.type_of_plot(X, Y) + plt.xlabel('Generation') + plt.ylabel('Highest Fitness') + plt.title('Relationship Between Generations and Highest Fitness') - self.plot() def lowest_value_chromosome(self): """Generation by Min value Chromosome """ @@ -96,16 +58,16 @@ class Matplotlib_Graph: generations = self.database.get_total_generations() # Create the generations list - [0,1,2,etc] - self.x = list(range(0, generations)) + X = list(range(0, generations)) # Query for Y data - self.y = self.database.get_lowest_chromosome() + Y = self.database.get_lowest_chromosome() - self.xlabel = 'Generation' - self.ylabel = 'Lowest Fitness' - self.title = 'Relationship Between Generations and Lowest Fitness' + self.type_of_plot(X, Y) + plt.xlabel('Generation') + plt.ylabel('Lowest Fitness') + plt.title('Relationship Between Generations and Lowest Fitness') - self.plot() # Getter and setters @property diff --git a/src/run_testing.py b/src/run_testing.py index 3d4e4d5..b9574fa 100644 --- a/src/run_testing.py +++ b/src/run_testing.py @@ -1,5 +1,6 @@ import EasyGA import random +import matplotlib.pyplot as plt # Create the Genetic algorithm ga = EasyGA.GA() @@ -7,10 +8,15 @@ ga = EasyGA.GA() # Create 25 chromosomes each with 10 genes and 200 generations ga.population_size = 100 ga.chromosome_length = 10 -ga.generation_goal = 1000 +ga.generation_goal = 150 ga.evolve() ga.print_population() -ga.graph.highest_value_chromosome() +plt.figure(figsize = [6, 6]) +ga.graph.highest_value_chromosome() # Change this so it doesn't make its own figure or show +plt.xlabel('days passed') # override the xlabel +plt.ylabel('products sold that day') # override the ylabel +plt.title('Efficiency over time') # override the title +plt.show()