本文介绍了在jupyter笔记本中内联后端的matplotlib配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何使用jupyter笔记本中的嵌入式后端为matplotlib配置默认值.具体来说,我想将默认的'figure.figsize'设置为[7.5,5.0],而不是默认的[6.0,4.0].我在配备matplotlib 1.4.3的Mac上使用jupyter notebook 1.1.

I'd like to learn how to configure the defaults for matplotlib using the inline backend in jupyter notebook. Specifically, I'd like to set default 'figure.figsize’ to [7.5, 5.0] instead of the default [6.0, 4.0]. I’m using jupyter notebook 1.1 on a Mac with matplotlib 1.4.3.

在笔记本中,使用macosx后端,我的matplotlibrc文件显示为在标准位置,并且figmat的设置如matplotlibrc中指定:

In the notebook, using the macosx backend, my matplotlibrc file is shown to be in the standard location, and figsize is set as specified in matplotlibrc:

In [1]: %matplotlib
Using matplotlib backend: MacOSX

In [2]: mpl.matplotlib_fname()
Out[2]: u'/Users/scott/.matplotlib/matplotlibrc'

In [3]: matplotlib.rcParams['figure.figsize']
Out[3]:[7.5, 5.0]

但是,当我使用嵌入式后端时,figsize的设置有所不同:

However, when I use the inline backend, figsize is set differently:

In [1]: %matplotlib inline

In [2]: mpl.matplotlib_fname()
Out[2]: u'/Users/scott/.matplotlib/matplotlibrc'

In [3]: matplotlib.rcParams['figure.figsize']
Out[3]:[6.0, 4.0]

在我的笔记本配置文件〜/.jupyter/jupyter_notebook_config.py中,我还添加了这一行

In my notebook config file, ~/.jupyter/jupyter_notebook_config.py, I also added the line

c.InlineBackend.rc = {'figure.figsize': (7.5, 5.0) }

但是这也不起作用.现在,我仍然在每个笔记本中添加以下行:

but this had no effect either. For now I’m stuck adding this line in every notebook:

matplotlib.rcParams['figure.figsize']=[7.5, 5.0]

有什么方法可以设置内联后端的默认值?

Is there any way to set the default for the inline backend?

推荐答案

Jupyter/IPython的划分令人困惑. Jupyter是内核的前端,其中IPython是事实上的Python内核.您正在尝试更改与matplotlib相关的内容,这仅在IPython内核的范围内才有意义.在~/.jupyter/jupyter_notebook_config.py中对matplotlib进行更改将适用于可能没有任何意义的 all 内核(在运行不使用matplotlib的Ruby/R/Bash/etc.内核的情况下).因此,您的c.InlineBackend.rc设置需要输入IPython内核的设置.

The Jupyter/IPython split is confusing. Jupyter is the front end to kernels, of which IPython is the defacto Python kernel. You are trying to change something related to matplotlib and this only makes sense within the scope of the IPython kernel. Making a change to matplotlib in ~/.jupyter/jupyter_notebook_config.py would apply to all kernels which may not make sense (in the case of running a Ruby/R/Bash/etc. kernel which doesn't use matplotlib). Therefore, your c.InlineBackend.rc setting needs to go in the settings for the IPython kernel.

编辑文件~/.ipython/profile_default/ipython_kernel_config.py并添加到底部:c.InlineBackend.rc = { }.

Edit the file ~/.ipython/profile_default/ipython_kernel_config.py and add to the bottom: c.InlineBackend.rc = { }.

由于c.InlineBackend.rc指定matplotlib配置 overrides ,因此空白命令告诉IPython内核不要覆盖任何.matplotlibrc设置.

Since c.InlineBackend.rc specifies matplotlib config overrides, the blank dict tells the IPython kernel not to override any of your .matplotlibrc settings.

如果文件不存在,请运行ipython profile create来创建它.

If the file doesn't exist, run ipython profile create to create it.

这篇关于在jupyter笔记本中内联后端的matplotlib配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 18:16
查看更多