diff --git a/pymoo/core/plot.py b/pymoo/core/plot.py index e9e7cfea..e782f1cf 100644 --- a/pymoo/core/plot.py +++ b/pymoo/core/plot.py @@ -31,12 +31,15 @@ def __init__(self, # change the font of plots to serif (looks better) plt.rc('font', family='serif') - # the matplotlib classes + # the matplotlib classes — publicly accessible after do()/show()/save() + # you can also use get_figure() and get_axes() helper methods self.fig = fig self.ax = ax self.figsize = figsize # whether the figure should be closed when object is destroyed + # set to False if you want to keep the figure alive after the Plot object + # goes out of scope (e.g. to further customise or display it) self.close_on_destroy = close_on_destroy # the title of the figure - can also be a list if subfigures @@ -172,10 +175,19 @@ def show(self, **kwargs): # in a notebook the plot method need not to be called explicitly if not in_notebook() and matplotlib.get_backend().lower() != "agg": plt.show(**kwargs) - plt.close() + # NOTE: plt.close() removed — callers can now close the figure themselves + # if needed, e.g.: plot.fig.savefig(...); plt.close(plot.fig) return self + def get_figure(self): + self.plot_if_not_done_yet() + return self.fig + + def get_axes(self): + self.plot_if_not_done_yet() + return self.ax + def save(self, fname, **kwargs): self.plot_if_not_done_yet() set_if_none(kwargs, "bbox_inches", "tight")