From 5be3c6df52e412b6b0c937c997ab770a3a9cd27c Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 28 Mar 2018 14:40:37 -0700 Subject: [PATCH] Rely a bit more on rc_context. --- lib/matplotlib/style/core.py | 9 ++--- lib/matplotlib/tests/test_backend_pgf.py | 42 +++++++++++------------- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/lib/matplotlib/style/core.py b/lib/matplotlib/style/core.py index 2a836721882b..650f80e6cb47 100644 --- a/lib/matplotlib/style/core.py +++ b/lib/matplotlib/style/core.py @@ -136,14 +136,11 @@ def context(style, after_reset=False): If True, apply style after resetting settings to their defaults; otherwise, apply style on top of the current settings. """ - initial_settings = mpl.rcParams.copy() - if after_reset: - mpl.rcdefaults() - try: + with mpl.rc_context(): + if after_reset: + mpl.rcdefaults() use(style) yield - finally: - mpl.rcParams.update(initial_settings) def load_base_library(): diff --git a/lib/matplotlib/tests/test_backend_pgf.py b/lib/matplotlib/tests/test_backend_pgf.py index 1a3d943cf3da..1bc49b056eed 100644 --- a/lib/matplotlib/tests/test_backend_pgf.py +++ b/lib/matplotlib/tests/test_backend_pgf.py @@ -117,30 +117,26 @@ def test_pdflatex(): @pytest.mark.style('default') @pytest.mark.backend('pgf') def test_rcupdate(): - rc_sets = [] - rc_sets.append({'font.family': 'sans-serif', - 'font.size': 30, - 'figure.subplot.left': .2, - 'lines.markersize': 10, - 'pgf.rcfonts': False, - 'pgf.texsystem': 'xelatex'}) - rc_sets.append({'font.family': 'monospace', - 'font.size': 10, - 'figure.subplot.left': .1, - 'lines.markersize': 20, - 'pgf.rcfonts': False, - 'pgf.texsystem': 'pdflatex', - 'pgf.preamble': ['\\usepackage[utf8x]{inputenc}', - '\\usepackage[T1]{fontenc}', - '\\usepackage{sfmath}']}) - tol = (6, 0) - original_params = mpl.rcParams.copy() + rc_sets = [{'font.family': 'sans-serif', + 'font.size': 30, + 'figure.subplot.left': .2, + 'lines.markersize': 10, + 'pgf.rcfonts': False, + 'pgf.texsystem': 'xelatex'}, + {'font.family': 'monospace', + 'font.size': 10, + 'figure.subplot.left': .1, + 'lines.markersize': 20, + 'pgf.rcfonts': False, + 'pgf.texsystem': 'pdflatex', + 'pgf.preamble': ['\\usepackage[utf8x]{inputenc}', + '\\usepackage[T1]{fontenc}', + '\\usepackage{sfmath}']}] + tol = [6, 0] for i, rc_set in enumerate(rc_sets): - mpl.rcParams.clear() - mpl.rcParams.update(original_params) - mpl.rcParams.update(rc_set) - create_figure() - compare_figure('pgf_rcupdate%d.pdf' % (i + 1), tol=tol[i]) + with mpl.rc_context(rc_set): + create_figure() + compare_figure('pgf_rcupdate%d.pdf' % (i + 1), tol=tol[i]) # test backend-side clipping, since large numbers are not supported by TeX