Updated plt usage

This commit is contained in:
SimpleArt
2020-11-12 21:16:17 -05:00
parent 7e0134e785
commit efb69f88ea
2 changed files with 27 additions and 59 deletions

View File

@ -12,44 +12,7 @@ class Matplotlib_Graph:
def __init__(self, database): def __init__(self, database):
self.database = database self.database = database
# Type self.type_of_plot = 'line'
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()
def generation_total_fitness(self): def generation_total_fitness(self):
@ -59,16 +22,15 @@ class Matplotlib_Graph:
generations = self.database.get_total_generations() generations = self.database.get_total_generations()
# Create the generations list - [0,1,2,etc] # Create the generations list - [0,1,2,etc]
self.x = list(range(0, generations)) X = list(range(0, generations))
# Query for Y data # Query for Y data
self.y = self.database.get_generation_total_fitness() Y = self.database.get_generation_total_fitness()
self.xlabel = 'Generation' self.type_of_plot(X, Y)
self.ylabel = 'Generation Total Fitness' plt.xlabel('Generation')
self.title = 'Relationship Between Generations and Generation Total Fitness' plt.ylabel('Generation Total Fitness')
plt.title('Relationship Between Generations and Generation Total Fitness')
self.plot()
def highest_value_chromosome(self): def highest_value_chromosome(self):
@ -78,16 +40,16 @@ class Matplotlib_Graph:
generations = self.database.get_total_generations() generations = self.database.get_total_generations()
# Create the generations list - [0,1,2,etc] # Create the generations list - [0,1,2,etc]
self.x = list(range(0, generations)) X = list(range(0, generations))
# Query for Y data # Query for Y data
self.y = self.database.get_highest_chromosome() Y = self.database.get_highest_chromosome()
self.xlabel = 'Generation' self.type_of_plot(X, Y)
self.ylabel = 'Highest Fitness' plt.xlabel('Generation')
self.title = 'Relationship Between Generations and Highest Fitness' plt.ylabel('Highest Fitness')
plt.title('Relationship Between Generations and Highest Fitness')
self.plot()
def lowest_value_chromosome(self): def lowest_value_chromosome(self):
"""Generation by Min value Chromosome """ """Generation by Min value Chromosome """
@ -96,16 +58,16 @@ class Matplotlib_Graph:
generations = self.database.get_total_generations() generations = self.database.get_total_generations()
# Create the generations list - [0,1,2,etc] # Create the generations list - [0,1,2,etc]
self.x = list(range(0, generations)) X = list(range(0, generations))
# Query for Y data # Query for Y data
self.y = self.database.get_lowest_chromosome() Y = self.database.get_lowest_chromosome()
self.xlabel = 'Generation' self.type_of_plot(X, Y)
self.ylabel = 'Lowest Fitness' plt.xlabel('Generation')
self.title = 'Relationship Between Generations and Lowest Fitness' plt.ylabel('Lowest Fitness')
plt.title('Relationship Between Generations and Lowest Fitness')
self.plot()
# Getter and setters # Getter and setters
@property @property

View File

@ -1,5 +1,6 @@
import EasyGA import EasyGA
import random import random
import matplotlib.pyplot as plt
# Create the Genetic algorithm # Create the Genetic algorithm
ga = EasyGA.GA() ga = EasyGA.GA()
@ -7,10 +8,15 @@ ga = EasyGA.GA()
# Create 25 chromosomes each with 10 genes and 200 generations # Create 25 chromosomes each with 10 genes and 200 generations
ga.population_size = 100 ga.population_size = 100
ga.chromosome_length = 10 ga.chromosome_length = 10
ga.generation_goal = 1000 ga.generation_goal = 150
ga.evolve() ga.evolve()
ga.print_population() 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()