From ccfe679c7881bb3a71d771aaf9da8777f9404818 Mon Sep 17 00:00:00 2001 From: rengel8 <34138513+rengel8@users.noreply.github.com> Date: Mon, 14 Jun 2021 23:01:09 +0200 Subject: [PATCH 1/2] Update pygad.py Allows to save the plot into a file as an option, since linux might throw an error showing it. --- pygad.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pygad.py b/pygad.py index 7ee11ca..d893d65 100644 --- a/pygad.py +++ b/pygad.py @@ -2530,7 +2530,7 @@ def best_solution(self, pop_fitness=None): return best_solution, best_solution_fitness, best_match_idx - def plot_result(self, title="PyGAD - Iteration vs. Fitness", xlabel="Generation", ylabel="Fitness", linewidth=3): + def plot_result(self, title="PyGAD - Iteration vs. Fitness", xlabel="Generation", ylabel="Fitness", linewidth=3, save_as=None): """ Creates and shows a plot that summarizes how the fitness value evolved by generation. Can only be called after completing at least 1 generation. If no generation is completed, an exception is raised. @@ -2540,6 +2540,7 @@ def plot_result(self, title="PyGAD - Iteration vs. Fitness", xlabel="Generation" xlabel: Label on the X-axis. ylabel: Label on the Y-axis. linewidth: Line width of the plot. + save_as: save image instead of showing it. Returns the figure. """ @@ -2555,7 +2556,12 @@ def plot_result(self, title="PyGAD - Iteration vs. Fitness", xlabel="Generation" matplotlib.pyplot.title(title) matplotlib.pyplot.xlabel(xlabel) matplotlib.pyplot.ylabel(ylabel) - matplotlib.pyplot.show() + + if save_as is None: + matplotlib.pyplot.show() # show plot + else: + fig.savefig(f'{save_as}.png') # save as image + return fig def save(self, filename): From 517612c4a5b66890d980ae247d7b291dc694524f Mon Sep 17 00:00:00 2001 From: Ahmed Gad Date: Mon, 14 Jun 2021 23:14:19 -0400 Subject: [PATCH 2/2] Two changes in the plot_result() method. 1) Rename the parameter to `save_dir`. If it is not None, then save. 2) Keep the .show() function called regardless of saving the figure or not. --- pygad.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pygad.py b/pygad.py index d893d65..5b2595b 100644 --- a/pygad.py +++ b/pygad.py @@ -2530,7 +2530,7 @@ def best_solution(self, pop_fitness=None): return best_solution, best_solution_fitness, best_match_idx - def plot_result(self, title="PyGAD - Iteration vs. Fitness", xlabel="Generation", ylabel="Fitness", linewidth=3, save_as=None): + def plot_result(self, title="PyGAD - Iteration vs. Fitness", xlabel="Generation", ylabel="Fitness", linewidth=3, save_dir=None): """ Creates and shows a plot that summarizes how the fitness value evolved by generation. Can only be called after completing at least 1 generation. If no generation is completed, an exception is raised. @@ -2540,7 +2540,7 @@ def plot_result(self, title="PyGAD - Iteration vs. Fitness", xlabel="Generation" xlabel: Label on the X-axis. ylabel: Label on the Y-axis. linewidth: Line width of the plot. - save_as: save image instead of showing it. + save_dir: Directory to save the figure. Returns the figure. """ @@ -2557,10 +2557,10 @@ def plot_result(self, title="PyGAD - Iteration vs. Fitness", xlabel="Generation" matplotlib.pyplot.xlabel(xlabel) matplotlib.pyplot.ylabel(ylabel) - if save_as is None: - matplotlib.pyplot.show() # show plot - else: - fig.savefig(f'{save_as}.png') # save as image + if not save_dir is None: + matplotlib.pyplot.savefig(fname=save_dir, + bbox_inches='tight') + matplotlib.pyplot.show() return fig @@ -2589,4 +2589,4 @@ def load(filename): raise FileNotFoundError("Error reading the file {filename}. Please check your inputs.".format(filename=filename)) except: raise BaseException("Error loading the file. Please check if the file exists.") - return ga_in \ No newline at end of file + return ga_in