Update matplotlib backend switching (#4987)

This commit is contained in:
Glenn Jocher 2023-09-20 00:50:30 +02:00 committed by GitHub
parent 28569ced8a
commit 7f78fad8ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 4 deletions

View file

@ -208,12 +208,16 @@ def plt_settings(rcparams=None, backend='Agg'):
def wrapper(*args, **kwargs):
"""Sets rc parameters and backend, calls the original function, and restores the settings."""
original_backend = plt.get_backend()
plt.switch_backend(backend)
if backend != original_backend:
plt.close('all') # auto-close()ing of figures upon backend switching is deprecated since 3.8
plt.switch_backend(backend)
with plt.rc_context(rcparams):
result = func(*args, **kwargs)
plt.switch_backend(original_backend)
if backend != original_backend:
plt.close('all')
plt.switch_backend(original_backend)
return result
return wrapper