Ensure matplotlib backend gets reset with plt_settings (#15759)
Co-authored-by: UltralyticsAssistant <web@ultralytics.com> Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
parent
8bb776fbc3
commit
a89b316f27
1 changed files with 10 additions and 7 deletions
|
|
@ -219,16 +219,19 @@ 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()
|
||||
if backend.lower() != original_backend.lower():
|
||||
switch = backend.lower() != original_backend.lower()
|
||||
if switch:
|
||||
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)
|
||||
|
||||
if backend != original_backend:
|
||||
plt.close("all")
|
||||
plt.switch_backend(original_backend)
|
||||
# Plot with backend and always revert to original backend
|
||||
try:
|
||||
with plt.rc_context(rcparams):
|
||||
result = func(*args, **kwargs)
|
||||
finally:
|
||||
if switch:
|
||||
plt.close("all")
|
||||
plt.switch_backend(original_backend)
|
||||
return result
|
||||
|
||||
return wrapper
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue