Python 设置样式表后如何恢复matplotlib默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26413185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:27:20  来源:igfitidea点击:

How to recover matplotlib defaults after setting stylesheet

pythonmatplotlib

提问by amd

In an ipython notebook, I used a matplotlib stylesheet to change the look of my plots using

在 ipython 笔记本中,我使用了 matplotlib 样式表来更改我的图的外观

from matplotlib.pyplot import *
%matplotlib inline
style.use('ggplot')

My version of matplotlib is 1.4.0. How do I go back to the default matplotlib styling? I tried all the available styles in

我的 matplotlib 版本是 1.4.0。我如何回到默认的 matplotlib 样式?我尝试了所有可用的样式

print style.available 

but there doesn't seem to be a "default" option. I also tried

但似乎没有“默认”选项。我也试过

matplotlib.rcdefaults() 

For some reason, this gives me a gray background. It also changes the text from gray (ggplot style) to black, which may be the default, but also could be another random style.

出于某种原因,这给了我一个灰色的背景。它还将文本从灰色(ggplot 样式)更改为黑色,这可能是默认值,但也可能是另一种随机样式。

采纳答案by CT Zhu

You should be able to set it back to default by:

您应该能够通过以下方式将其设置回默认值:

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

In ipython, things are a little different, especially with inlinebackend:

在 中ipython,情况有所不同,尤其是inline后端:

In [1]:

%matplotlib inline
In [2]:

import matplotlib as mpl
import matplotlib.pyplot as plt
In [3]:

inline_rc = dict(mpl.rcParams)
In [4]:

plt.plot(range(10))
Out[4]:
[<matplotlib.lines.Line2D at 0x72d2510>]

enter image description here

在此处输入图片说明

In [5]:

mpl.rcParams.update(mpl.rcParamsDefault)
plt.plot(range(10))
Out[5]:
[<matplotlib.lines.Line2D at 0x7354730>]

enter image description here

在此处输入图片说明

In [6]:

mpl.rcParams.update(inline_rc)
plt.plot(range(10))
Out[6]:
[<matplotlib.lines.Line2D at 0x75a8e10>] 

enter image description here

在此处输入图片说明

Basically, %matplotlib inlineuses its own rcParams. You can grab that from the source, but the arguably easier way is probably just save the rcParamsas inline_rcafter %matplotlib inlinecell magic in this example, and reuse that later.

基本上,%matplotlib inline使用它自己的rcParams. 您可以从源代码中获取它,但可以说更简单的方法可能只是将本示例中的rcParamsas inline_rcafter %matplotlib inlinecell magic保存下来,然后再重用。

回答by dequation

There actually is a default. But it's not listed under plt.style.available. Simply run :

实际上有一个default. 但它没有列在plt.style.available. 只需运行:

plt.style.use('default')

It returns the style to the default Matplotlib settings in, for instance, Jupyter Notebook.

它将样式返回到默认的 Matplotlib 设置,例如 Jupyter Notebook。

回答by sancho.s ReinstateMonicaCellio

Adding to the answer by CT Zhu, the differences between the inlineand matplotlibdefaults are (for each item that is different, a list with the to respective values is given):

添加到CT Zhu答案中, theinlinematplotlibdefaults之间的差异是(对于每个不同的项目,给出了各自值的列表):

inline_default_rc = dict(mpl.rcParams)
default_rc = dict(mpl.rcParamsDefault)
print( {k:[v,default_rc[k]] for k,v in inline_default_rc.items() if v != default_rc[k]} )

{'figure.dpi': [72.0, 100.0], 'figure.edgecolor': [(1, 1, 1, 0), 'white'], 'figure.facecolor': [(1, 1, 1, 0), 'white'], 'figure.figsize': [[6.0, 4.0], [6.4, 4.8]], 'figure.subplot.bottom': [0.125, 0.11], 'interactive': [True, False]}

You can use this to fine tune your plots.

您可以使用它来微调您的绘图。