将 matplotlib.rcParams 字典正确重置为其原始默认值 [英] Properly reset matplotlib.rcParams dictionary to its original defaults

查看:272
本文介绍了将 matplotlib.rcParams 字典正确重置为其原始默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此答案提到了其中一个

fig = plt.figure()
fig.patch.set_facecolor('black')

plt.rcParams['figure.facecolor'] = 'black'

将更改rcParams词典中键"figure.facecolor"的值.

will change the value in the rcParams dictionary for the key 'figure.facecolor'.

假设我的脚本基于用户交互以不确定的方式对值进行了多次更改,而我想撤消所有这些并返回到matplotlib的默认参数和行为.

Suppose that my script has made several changes to the values in a nondeterministic way based on user interaction, and I want to undo all of that and go back to matplotlib's default parameters and behavior.

在脚本的开头,我可以检查 matplotlib.rcParams 并存储整个字典或某些键的值,然后一次或使用还原它们.update() 方法,但我不知道这是否明智,因为我不知道如何使用 matplotlib.RcParams 实例(它不仅仅是字典).它确实有一个 .setdefault() 方法,但我不明白什么帮助返回:

In the beginning of the script I could check matplotlib.rcParams and store either the whole dictionary, or values for certain keys, and then restore them one at a time or with the .update() method, but I don't know if that's wise because I don't know how else the matplotlib.RcParams instance is used (it's not just a dictionary). It does have a .setdefault() method but I can't understand what help returns on that:

Help on method setdefault in module collections.abc:

setdefault(key, default=None) method of matplotlib.RcParams instance
    D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D

是否有某种恢复原始默认值功能,或者我应该通过使用我存储的副本更新整个内容来实现吗?

Is there some kind of restore the original default values feature, or should I just wing-it by updating the whole thing with the copy that I've stored?

推荐答案

根据我对设置后如何恢复matplotlib默认值的理解和回答样式表,您应该可以执行以下操作:

Per my understanding and answers to How to recover matplotlib defaults after setting stylesheet you should be able to do this:

import matplotlib
matplotlib.rcParams.update(matplotlib.rcParamsDefault)

您还可以检查 site-packages/matplotlib/mpl-data 文件夹中名为 matplotlibrc 的文件.它应该有完整的默认值.

You could also check the site-packages/matplotlib/mpl-data folder for the file named matplotlibrc. It should have the entire default values there.

这篇关于将 matplotlib.rcParams 字典正确重置为其原始默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆