Skip to content

Commit 00ae375

Browse files
authored
Merge pull request #10913 from anntzer/rc_context
Rely a bit more on rc_context.
2 parents 980f6ef + 5be3c6d commit 00ae375

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

lib/matplotlib/style/core.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,11 @@ def context(style, after_reset=False):
136136
If True, apply style after resetting settings to their defaults;
137137
otherwise, apply style on top of the current settings.
138138
"""
139-
initial_settings = mpl.rcParams.copy()
140-
if after_reset:
141-
mpl.rcdefaults()
142-
try:
139+
with mpl.rc_context():
140+
if after_reset:
141+
mpl.rcdefaults()
143142
use(style)
144143
yield
145-
finally:
146-
mpl.rcParams.update(initial_settings)
147144

148145

149146
def load_base_library():

lib/matplotlib/tests/test_backend_pgf.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -117,30 +117,26 @@ def test_pdflatex():
117117
@pytest.mark.style('default')
118118
@pytest.mark.backend('pgf')
119119
def test_rcupdate():
120-
rc_sets = []
121-
rc_sets.append({'font.family': 'sans-serif',
122-
'font.size': 30,
123-
'figure.subplot.left': .2,
124-
'lines.markersize': 10,
125-
'pgf.rcfonts': False,
126-
'pgf.texsystem': 'xelatex'})
127-
rc_sets.append({'font.family': 'monospace',
128-
'font.size': 10,
129-
'figure.subplot.left': .1,
130-
'lines.markersize': 20,
131-
'pgf.rcfonts': False,
132-
'pgf.texsystem': 'pdflatex',
133-
'pgf.preamble': ['\\usepackage[utf8x]{inputenc}',
134-
'\\usepackage[T1]{fontenc}',
135-
'\\usepackage{sfmath}']})
136-
tol = (6, 0)
137-
original_params = mpl.rcParams.copy()
120+
rc_sets = [{'font.family': 'sans-serif',
121+
'font.size': 30,
122+
'figure.subplot.left': .2,
123+
'lines.markersize': 10,
124+
'pgf.rcfonts': False,
125+
'pgf.texsystem': 'xelatex'},
126+
{'font.family': 'monospace',
127+
'font.size': 10,
128+
'figure.subplot.left': .1,
129+
'lines.markersize': 20,
130+
'pgf.rcfonts': False,
131+
'pgf.texsystem': 'pdflatex',
132+
'pgf.preamble': ['\\usepackage[utf8x]{inputenc}',
133+
'\\usepackage[T1]{fontenc}',
134+
'\\usepackage{sfmath}']}]
135+
tol = [6, 0]
138136
for i, rc_set in enumerate(rc_sets):
139-
mpl.rcParams.clear()
140-
mpl.rcParams.update(original_params)
141-
mpl.rcParams.update(rc_set)
142-
create_figure()
143-
compare_figure('pgf_rcupdate%d.pdf' % (i + 1), tol=tol[i])
137+
with mpl.rc_context(rc_set):
138+
create_figure()
139+
compare_figure('pgf_rcupdate%d.pdf' % (i + 1), tol=tol[i])
144140

145141

146142
# test backend-side clipping, since large numbers are not supported by TeX

0 commit comments

Comments
 (0)