Skip to content

Commit 97fcbd6

Browse files
committed
ENH/FIX: Allow rc_context to ignore keys on reset
The motivating issue is that if the backend was the auto backend sentinel and the backend was first fully resolved in an `rc_context` block, then we would reset back to the sentinel an exit which would lead to strange behavior with figures being erroneously closed. Adding a general purpose exclusion list seems like a better option than special casing either the backend key or the backend value. Closes matplotlib#23298
1 parent d58e966 commit 97fcbd6

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

‎lib/matplotlib/__init__.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ def rc_file(fname, *, use_default_template=True):
10551055

10561056

10571057
@contextlib.contextmanager
1058-
def rc_context(rc=None, fname=None):
1058+
def rc_context(rc=None, fname=None, *, exclude_list=('backend',)):
10591059
"""
10601060
Return a context manager for temporarily changing rcParams.
10611061
@@ -1066,6 +1066,9 @@ def rc_context(rc=None, fname=None):
10661066
fname : str or path-like
10671067
A file with Matplotlib rc settings. If both *fname* and *rc* are given,
10681068
settings from *rc* take precedence.
1069+
exclude_list : Iterable[str], optional
1070+
A sequence of keys which should not be re-set on exit. By default this
1071+
is only the backend.
10691072
10701073
See Also
10711074
--------
@@ -1087,7 +1090,9 @@ def rc_context(rc=None, fname=None):
10871090
plt.plot(x, y) # uses 'print.rc'
10881091
10891092
"""
1090-
orig = rcParams.copy()
1093+
orig = dict(rcParams.copy())
1094+
for k in exclude_list:
1095+
del orig[k]
10911096
try:
10921097
if fname:
10931098
rc_file(fname)

0 commit comments

Comments
 (0)