Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When to .show()? #6

Open
grst opened this issue Feb 28, 2024 · 0 comments
Open

When to .show()? #6

grst opened this issue Feb 28, 2024 · 0 comments

Comments

@grst
Copy link
Collaborator

grst commented Feb 28, 2024

Our plotting functions should work well in different environments, most prominently

  • jupyter notebooks
  • an interactive python shell, such as ipython.

Whether the plot is shown or not should be configurable via settings and/or context manager. But we should come up with reasonable defaults.

When ax is provided

Never show the figure. In that case we assume that the user is building a larger figure consisting of multiple panels. They should invoke plt.show() once they are ready. It is debatable whether or not we should return the figure object in that case (see jupyter notebook case below).

Jupyter notebooks

Jupyter notebooks automatically show a Figure object that is returned by the last statement in a cell. Otherwise it can be shown with display(fig):

plot_umap()
print("foo")

doesn't show the figure, but

display(plot_umap())
print("foo")

and

print("foo")
plot_umap()

do.

This matches my expectation quite well, as it would be the same for a function returning e.g. a pandas data frame.
The function should also call plt.close() before returning the figure, otherwise too many open figures accumulate.

Interactive environments.

In an interactive environment, I'd expect

pl.umap()

to actually

  • show the plot
  • clean up once the plot is closed (otherwise too many open figures accumulate).

This can be achieved by plt.show(). Figure.show() does not work if the plot has been closed before using plt.close() as we should do within the function (unless ax is provided).

@flying-sheep, this is why I proposed to call either plt.close() or plt.show() depending on the backend (see also #1).
Would be great to hear your thoughts on this! Also are there any other relevant environments that I'm missing?

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

No branches or pull requests

1 participant