jupyter notebook 中内联后端的 matplotlib 配置 [英] matplotlib configuration for inline backend in jupyter notebook

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

问题描述

我想学习如何使用 jupyter notebook 中的内联后端为 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.

在 notebook 中,使用 macosx 后端,我的 matplotlibrc 文件显示在标准位置,并且 figsize 设置为 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 进行更改将适用于 所有 内核,这可能没有意义(在运行 Ruby/R/Bash 的情况下)/etc. 不使用 matplotlib 的内核).因此,您的 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 notebook 中内联后端的 matplotlib 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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