Skip to content

Commit 517612c

Browse files
authored
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.
1 parent ccfe679 commit 517612c

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

pygad.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -2530,7 +2530,7 @@ def best_solution(self, pop_fitness=None):
25302530

25312531
return best_solution, best_solution_fitness, best_match_idx
25322532

2533-
def plot_result(self, title="PyGAD - Iteration vs. Fitness", xlabel="Generation", ylabel="Fitness", linewidth=3, save_as=None):
2533+
def plot_result(self, title="PyGAD - Iteration vs. Fitness", xlabel="Generation", ylabel="Fitness", linewidth=3, save_dir=None):
25342534

25352535
"""
25362536
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"
25402540
xlabel: Label on the X-axis.
25412541
ylabel: Label on the Y-axis.
25422542
linewidth: Line width of the plot.
2543-
save_as: save image instead of showing it.
2543+
save_dir: Directory to save the figure.
25442544
25452545
Returns the figure.
25462546
"""
@@ -2557,10 +2557,10 @@ def plot_result(self, title="PyGAD - Iteration vs. Fitness", xlabel="Generation"
25572557
matplotlib.pyplot.xlabel(xlabel)
25582558
matplotlib.pyplot.ylabel(ylabel)
25592559

2560-
if save_as is None:
2561-
matplotlib.pyplot.show() # show plot
2562-
else:
2563-
fig.savefig(f'{save_as}.png') # save as image
2560+
if not save_dir is None:
2561+
matplotlib.pyplot.savefig(fname=save_dir,
2562+
bbox_inches='tight')
2563+
matplotlib.pyplot.show()
25642564

25652565
return fig
25662566

@@ -2589,4 +2589,4 @@ def load(filename):
25892589
raise FileNotFoundError("Error reading the file {filename}. Please check your inputs.".format(filename=filename))
25902590
except:
25912591
raise BaseException("Error loading the file. Please check if the file exists.")
2592-
return ga_in
2592+
return ga_in

0 commit comments

Comments
 (0)