jupyter 笔记本中内联后端的 matplotlib 配置
matplotlib configuration for inline backend in jupyter notebook
我想了解如何使用 jupyter notebook 中的内联后端配置 matplotlib 的默认设置。具体来说,我想将默认值 'figure.figsize' 设置为 [7.5, 5.0] 而不是默认值 [6.0, 4.0]。我在带有 matplotlib 1.4.3 的 Mac 上使用 jupyter notebook 1.1。
在笔记本中,使用 macosx 后端,我的 matplotlibrc 文件显示在标准位置,并且 figsize 设置为 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 设置不同:
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 中,我还添加了行
c.InlineBackend.rc = {'figure.figsize': (7.5, 5.0) }
但这也没有效果。现在我坚持在每个笔记本中添加这一行:
matplotlib.rcParams['figure.figsize']=[7.5, 5.0]
有什么方法可以设置内联后端的默认值吗?
Jupyter/IPython 拆分令人困惑。 Jupyter 是内核的前端,其中 IPython 是事实上的 Python 内核。您正在尝试更改与 matplotlib 相关的内容,这仅在 IPython 内核范围内才有意义。在 ~/.jupyter/jupyter_notebook_config.py
中对 matplotlib 进行更改将适用于 所有 内核,这可能没有意义(在 运行 宁一个 Ruby/R/Bash/etc 内核的情况下不使用 matplotlib)。因此,您的 c.InlineBackend.rc
设置需要进入 IPython 内核的设置。
编辑文件~/.ipython/profile_default/ipython_kernel_config.py
并添加到底部:c.InlineBackend.rc = { }
.
由于c.InlineBackend.rc
指定matplotlib配置覆盖,空白字典告诉IPython内核不要覆盖任何.matplotlibrc设置。
如果文件不存在,运行 ipython profile create
创建它。
在顶部单元格中使用 figsize(width,height) 并更改后续绘图的宽度
至少在 windows 上使用 Jupyter,我能够使用非常类似于 venkat 的答案的东西来做到这一点,即:
%matplotlib inline
import matplotlib
matplotlib.rcParams['figure.figsize'] = (8, 8)
我这样做是为了使圆变成方形,到那时它一直是椭圆形的。看,圆的平方并不难。 :)
对于带有 IPython 内核的 jupyter 5.x 及更高版本,您可以只覆盖特定的键并通过放置这样的东西来保留其余部分,将您想要的 figsize
放入您的 ~/.ipython/profile_default/ipython_kernel_config.py
:
c = get_config()
c.InlineBackend.rc.update({"figure.figsize": (12, 10)})
请注意,如果您 运行 ipython
来自虚拟环境,ipython_kernel_config.py
的路径会有所不同。那样的话,挖环境存放的路径。
我想了解如何使用 jupyter notebook 中的内联后端配置 matplotlib 的默认设置。具体来说,我想将默认值 'figure.figsize' 设置为 [7.5, 5.0] 而不是默认值 [6.0, 4.0]。我在带有 matplotlib 1.4.3 的 Mac 上使用 jupyter notebook 1.1。
在笔记本中,使用 macosx 后端,我的 matplotlibrc 文件显示在标准位置,并且 figsize 设置为 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 设置不同:
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 中,我还添加了行
c.InlineBackend.rc = {'figure.figsize': (7.5, 5.0) }
但这也没有效果。现在我坚持在每个笔记本中添加这一行:
matplotlib.rcParams['figure.figsize']=[7.5, 5.0]
有什么方法可以设置内联后端的默认值吗?
Jupyter/IPython 拆分令人困惑。 Jupyter 是内核的前端,其中 IPython 是事实上的 Python 内核。您正在尝试更改与 matplotlib 相关的内容,这仅在 IPython 内核范围内才有意义。在 ~/.jupyter/jupyter_notebook_config.py
中对 matplotlib 进行更改将适用于 所有 内核,这可能没有意义(在 运行 宁一个 Ruby/R/Bash/etc 内核的情况下不使用 matplotlib)。因此,您的 c.InlineBackend.rc
设置需要进入 IPython 内核的设置。
编辑文件~/.ipython/profile_default/ipython_kernel_config.py
并添加到底部:c.InlineBackend.rc = { }
.
由于c.InlineBackend.rc
指定matplotlib配置覆盖,空白字典告诉IPython内核不要覆盖任何.matplotlibrc设置。
如果文件不存在,运行 ipython profile create
创建它。
在顶部单元格中使用 figsize(width,height) 并更改后续绘图的宽度
至少在 windows 上使用 Jupyter,我能够使用非常类似于 venkat 的答案的东西来做到这一点,即:
%matplotlib inline
import matplotlib
matplotlib.rcParams['figure.figsize'] = (8, 8)
我这样做是为了使圆变成方形,到那时它一直是椭圆形的。看,圆的平方并不难。 :)
对于带有 IPython 内核的 jupyter 5.x 及更高版本,您可以只覆盖特定的键并通过放置这样的东西来保留其余部分,将您想要的 figsize
放入您的 ~/.ipython/profile_default/ipython_kernel_config.py
:
c = get_config()
c.InlineBackend.rc.update({"figure.figsize": (12, 10)})
请注意,如果您 运行 ipython
来自虚拟环境,ipython_kernel_config.py
的路径会有所不同。那样的话,挖环境存放的路径。