2

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!

2
  • ga_instance.save(filename=filename)According to the manual Exsample, this is what saving to a file looks like. Commented Feb 5, 2021 at 3:30
  • Hey, thanks for your answer but that saves the instance of the ga, not the resulting plot, which is what I'm interested about... Commented Feb 5, 2021 at 8:38

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.