使用`with`恢复Matplotlib默认样式`

使用`with`恢复Matplotlib默认样式`,matplotlib,with-statement,Matplotlib,With Statement,我希望能够暂时使用这个习惯用法为Matplotlib绘图使用任意的订书钉 with my_styles(style): # plotting code ... 并定义了 def my_styles(style='mine'): if style == 'mine': return plt.rc_context({ "font.family": "sans-serif", &

我希望能够暂时使用这个习惯用法为Matplotlib绘图使用任意的订书钉

with my_styles(style):
    # plotting code ...
并定义了

def my_styles(style='mine'):
    if style == 'mine':
        return plt.rc_context({
            "font.family": "sans-serif", 
            "mathtext.fontset": "dejavusans", 
            "font.sans-serif": ["Nunito", "Open Sans"] + plt.rcParams["font.sans-serif"],
            "text.usetex": False })
    elif style == 'another': 
        # USE - Very slow (?)
        return plt.rc_context({
            'axes.linewidth': 1.5,
            'lines.linewidth': 2.0,
            'figure.facecolor': 'white',
            'grid.linewidth': 0.0,
            'axes.grid': False,
            'axes.unicode_minus': False,
            'axes.edgecolor': 'black',
            'text.usetex': True
        })
    else:
        return plt.rc_context(plt.rc_params())

但最后一种情况不会恢复Matplotlib的默认样式,而是继续使用以前使用的样式。如何完全恢复Matplotlib的默认值,使用
rc\u context
with
执行此操作的正确习惯用法是什么?

help@tmdavison精心设计。这就是以前的版本。可以help@tmdavison精心设计。这就是以前的版本。