I'm actually working on a project using google colaboratory. I'm using the pygad module(a Genetic Algorithm module). After running the algorithm, one can obtain the plot of a function, the fitness function, in such a way:
resultplot = ga_instance.plot_result()
Which returns the plot when executing the cell. However, the output of the function is None
. If I use the resultplot.savefig('plot.png')
function in this case I get an error.
Is there another way of saving the image with a command? Without having to use left click + save image as.
Thanks!
Unfortunately, the plot_result()
method does not return the created figure and thus you cannot save it. But do not worry, you can still save the figure.
The plot_result()
simply plots the data saved in the best_solutions_fitness
attribute. This attribute can be accessed anywhere using the instance of the pygad.GA
class.
You can simply use the best_solutions_fitness
attribute to rebuild the figure and save it. Here is what you should do:
Rather than calling the plot_result()
method, simply use the next code which creates the figure, shows the same plot created using the plot_result()
method, and saves it.
import matplotlib.pyplot
matplotlib.pyplot.figure()
matplotlib.pyplot.plot(ga_instance.best_solutions_fitness)
matplotlib.pyplot.savefig("PyGAD_figure.jpg")
matplotlib.pyplot.show()