Skip to content

fix: remove plt.close() from show(), expose get_figure() and get_axes()#787

Open
katerinakzmn wants to merge 2 commits into
anyoptimization:mainfrom
katerinakzmn:fix/expose-figure-axes-handles
Open

fix: remove plt.close() from show(), expose get_figure() and get_axes()#787
katerinakzmn wants to merge 2 commits into
anyoptimization:mainfrom
katerinakzmn:fix/expose-figure-axes-handles

Conversation

@katerinakzmn
Copy link
Copy Markdown

Fixes #771.

Previously, calling .show() would call plt.close() immediately after plt.show(), destroying the figure and making it impossible to:

  • further customise the plot (add titles, annotations, change limits)
  • save it again with different settings
  • access the figure/axes objects for downstream use

Changes:

  • Remove the bare plt.close() call from Plot.show()
  • Add Plot.get_figure() -> matplotlib.figure.Figure
  • Add Plot.get_axes() -> matplotlib.axes.Axes (or ndarray)
  • Clarify close_on_destroy comment: users can set it to False to keep
    the figure alive after the Plot object is garbage-collected

Users who relied on plt.close() being called automatically can close explicitly after they are done:
plot.show()
plt.close(plot.fig)

Fixes anyoptimization#771.

Previously, calling .show() would call plt.close() immediately after
plt.show(), destroying the figure and making it impossible to:
  - further customise the plot (add titles, annotations, change limits)
  - save it again with different settings
  - access the figure/axes objects for downstream use

Changes:
  - Remove the bare plt.close() call from Plot.show()
  - Add Plot.get_figure() -> matplotlib.figure.Figure
  - Add Plot.get_axes()   -> matplotlib.axes.Axes (or ndarray)
  - Clarify close_on_destroy comment: users can set it to False to keep
    the figure alive after the Plot object is garbage-collected

Users who relied on plt.close() being called automatically can close
explicitly after they are done:
    plot.show()
    plt.close(plot.fig)
@katerinakzmn katerinakzmn reopened this Jun 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Remove plt.close() from the plotting code or otherwise expose the figure / axes handles

1 participant