Skip to content

Instantly share code, notes, and snippets.

@cwindolf
Created June 21, 2023 17:27
Show Gist options
  • Save cwindolf/75a03aa466726f8149ddcd065a51a923 to your computer and use it in GitHub Desktop.
Save cwindolf/75a03aa466726f8149ddcd065a51a923 to your computer and use it in GitHub Desktop.
Matplotlib context manager helpers
import matplotlib.pyplot as plt
import contextlib
@contextlib.contextmanager
def subplots(*args, **kwargs):
fig, axes = plt.subplots(*args, **kwargs)
try:
yield fig, axes
finally:
plt.show()
plt.close(fig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment