Python “%matplotlib 内联”的目的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43027980/
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
Purpose of "%matplotlib inline"
提问by Rishabh
Could someone explain to me what exactly is the use of %matplotlib inline
?
有人可以向我解释一下究竟是什么用途%matplotlib inline
吗?
回答by Aurora0001
%matplotlib
is a magic functionin IPython. I'll quote the relevant documentation here for you to read for convenience:
%matplotlib
是IPython 中的魔法函数。为方便起见,我将在此处引用相关文档供您阅读:
IPython has a set of predefined ‘magic functions' that you can call with a command line style syntax. There are two kinds of magics, line-oriented and cell-oriented. Line magics are prefixed with the % character and work much like OS command-line calls: they get as an argument the rest of the line, where arguments are passed without parentheses or quotes. Lines magics can return results and can be used in the right hand side of an assignment. Cell magics are prefixed with a double %%, and they are functions that get as an argument not only the rest of the line, but also the lines below it in a separate argument.
IPython 有一组预定义的“魔法函数”,您可以使用命令行样式语法调用它们。有两种魔法,面向线和面向细胞。行魔法以 % 字符为前缀,其工作方式与 OS 命令行调用非常相似:它们将行的其余部分作为参数获取,其中参数在传递时不带括号或引号。Lines magics 可以返回结果,并且可以在赋值的右侧使用。Cell magics 以双 %% 为前缀,它们是不仅将行的其余部分作为参数,而且在单独的参数中将其下方的行作为参数的函数。
%matplotlib inline
sets the backend of matplotlib to the 'inline' backend:
%matplotlib inline
将 matplotlib 的后端设置为“内联”后端:
With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.
使用此后端,绘图命令的输出内联显示在 Jupyter 笔记本等前端内,直接在生成它的代码单元下方。结果图也将存储在笔记本文档中。
When using the 'inline' backend, your matplotlib graphs will be included in your notebook, next to the code. It may be worth also reading How to make IPython notebook matplotlib plot inlinefor reference on how to use it in your code.
使用“内联”后端时,您的 matplotlib 图形将包含在您的笔记本中,代码旁边。可能还值得阅读如何使 IPython notebook matplotlib plot inline以供参考,以了解如何在代码中使用它。
If you want interactivity as well, you can use the nbagg backendwith %matplotlib notebook
(in IPython 3.x), as described here.
如果你想交互,以及,你可以使用nbagg后端与%matplotlib notebook
(在IPython中3.X),如所描述这里。
回答by matusko
Provided you are running IPython, the %matplotlib inline
will make your plot outputs appear and be stored within the notebook.
如果您正在运行 IPython,它%matplotlib inline
将使您的绘图输出出现并存储在笔记本中。
According to documentation
根据文档
To set this up, before any plotting or import of
matplotlib
is performed you must execute the%matplotlib magic command
. This performs the necessary behind-the-scenes setup for IPython to work correctly hand in hand withmatplotlib
; it does not, however, actually execute any Python import commands, that is, no names are added to the namespace.A particularly interesting backend, provided by IPython, is the
inline
backend. This is available only for the Jupyter Notebook and the Jupyter QtConsole. It can be invoked as follows:%matplotlib inline
With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.
要进行设置,在执行任何绘图或导入之前,
matplotlib
您必须执行%matplotlib magic command
. 这为 IPython 执行必要的幕后设置,以便与matplotlib
; 然而,它实际上并没有执行任何 Python 导入命令,也就是说,没有名称被添加到命名空间中。IPython 提供的一个特别有趣的后端是
inline
后端。这仅适用于 Jupyter Notebook 和 Jupyter QtConsole。它可以被调用如下:%matplotlib inline
使用此后端,绘图命令的输出内联显示在 Jupyter 笔记本等前端内,直接在生成它的代码单元下方。结果图也将存储在笔记本文档中。
回答by Yossarian42
If you want to add plots to your Jupyter notebook, then %matplotlib inline
is a standard solution. And there are other magic commands will use matplotlib
interactively within Jupyter.
如果您想在 Jupyter 笔记本中添加绘图,那么这%matplotlib inline
是一个标准解决方案。还有其他魔法命令将matplotlib
在 Jupyter 中交互使用。
%matplotlib
: any plt
plot command will now cause a figure window to open, and further commands can be run to update the plot. Some changes will not draw automatically, to force an update, use plt.draw()
%matplotlib
:plt
现在任何绘图命令都会打开一个图形窗口,并且可以运行进一步的命令来更新绘图。某些更改不会自动绘制,要强制更新,请使用plt.draw()
%matplotlib notebook
: will lead to interactive plots embedded within the notebook, you can zoom and resize the figure
%matplotlib notebook
: 将导致嵌入笔记本中的交互式绘图,您可以缩放和调整图形大小
%matplotlib inline
: only draw static images in the notebook
%matplotlib inline
: 只在笔记本中绘制静态图像
回答by Vicki B
Starting with IPython 5.0 and matplotlib 2.0 you can avoid the use of IPython's specific magic and use
matplotlib.pyplot.ion()/matplotlib.pyplot.ioff()
which have the advantages of working outside of IPython as well.
从 IPython 5.0 和 matplotlib 2.0 开始,您可以避免使用 IPython 的特定魔法和使用
matplotlib.pyplot.ion()/matplotlib.pyplot.ioff()
,它们也具有在 IPython 之外工作的优势。
回答by Arushi Jain
It just means that any graph which we are creating as a part of our code will appear in the same notebook and not in separate window which would happen if we have not used this magic statement.
这只是意味着我们作为代码的一部分创建的任何图形都将出现在同一个笔记本中,而不是出现在单独的窗口中,如果我们没有使用这个魔术语句就会发生这种情况。
回答by Vihang Khare
If you don't know what backend is , you can read this: https://matplotlib.org/tutorials/introductory/usage.html#backends
如果你不知道什么是后端,你可以阅读这个:https: //matplotlib.org/tutorials/introductory/usage.html#backends
Some people use matplotlib interactively from the python shell and have plotting windows pop up when they type commands. Some people run Jupyter notebooks and draw inline plots for quick data analysis. Others embed matplotlib into graphical user interfaces like wxpython or pygtk to build rich applications. Some people use matplotlib in batch scripts to generate postscript images from numerical simulations, and still others run web application servers to dynamically serve up graphs. To support all of these use cases, matplotlib can target different outputs, and each of these capabilities is called a backend; the "frontend" is the user facing code, i.e., the plotting code, whereas the "backend" does all the hard work behind-the-scenes to make the figure.
有些人从 python shell 交互式地使用 matplotlib,并在他们键入命令时弹出绘图窗口。有些人运行 Jupyter notebook 并绘制内联图以进行快速数据分析。其他人将 matplotlib 嵌入到 wxpython 或 pygtk 等图形用户界面中以构建丰富的应用程序。有些人在批处理脚本中使用 matplotlib 从数值模拟中生成 postscript 图像,还有一些人运行 Web 应用程序服务器来动态提供图形。为了支持所有这些用例,matplotlib 可以针对不同的输出,这些功能中的每一个都称为后端;“前端”是面向用户的代码,即绘图代码,而“后端”则在幕后完成所有艰苦的工作来制作图形。
So when you type %matplotlib inline , it activates the inline backend. As discussed in the previous posts :
因此,当您键入 %matplotlib inline 时,它会激活内联后端。正如之前的帖子所讨论的:
With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.
使用此后端,绘图命令的输出内联显示在 Jupyter 笔记本等前端内,直接在生成它的代码单元下方。结果图也将存储在笔记本文档中。
回答by prosti
回答by Ani Menon
TL;DR
TL; 博士
%matplotlib inline
- Displays output inline
%matplotlib inline
- 内联显示输出
IPython kernel has the ability to display plots by executing code. The IPython kernel is designed to work seamlessly with the matplotlib plotting library to provide this functionality.
IPython 内核能够通过执行代码来显示绘图。IPython 内核旨在与 matplotlib 绘图库无缝协作以提供此功能。
%matplotlib
is a magic command which performs the necessary behind-the-scenes setup for IPython to work correctly hand-in-hand withmatplotlib
; it does not execute any Python import commands, that is, no names are added to the namespace.
%matplotlib
是一个神奇的命令,它执行必要的幕后设置,使 IPython 与matplotlib
; 它不执行任何 Python 导入命令,即不向命名空间添加任何名称。
Display output in separate window
在单独的窗口中显示输出
%matplotlib
Display output inline
显示输出内联
(available only for the Jupyter Notebook and the Jupyter QtConsole)
(仅适用于 Jupyter Notebook 和 Jupyter QtConsole)
%matplotlib inline
Display with interactive backends
显示与交互式后端
(valid values 'GTK3Agg', 'GTK3Cairo', 'MacOSX', 'nbAgg', 'Qt4Agg', 'Qt4Cairo', 'Qt5Agg', 'Qt5Cairo', 'TkAgg', 'TkCairo', 'WebAgg', 'WX', 'WXAgg', 'WXCairo', 'agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template'
)
(有效值'GTK3Agg', 'GTK3Cairo', 'MacOSX', 'nbAgg', 'Qt4Agg', 'Qt4Cairo', 'Qt5Agg', 'Qt5Cairo', 'TkAgg', 'TkCairo', 'WebAgg', 'WX', 'WXAgg', 'WXCairo', 'agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template'
)
%matplotlib gtk
Example - GTK3Agg - An Agg rendering to a GTK 3.x canvas (requires PyGObject and pycairo or cairocffi).
示例 - GTK3Agg - Agg 渲染到 GTK 3.x 画布(需要 PyGObject 和 pycairo 或 cairocffi)。
More details about matplotlib interactive backends: here
有关 matplotlib 交互式后端的更多详细信息:此处
Starting with
IPython 5.0
andmatplotlib 2.0
you can avoid the use of IPython's specific magic and usematplotlib.pyplot.ion()
/matplotlib.pyplot.ioff()
which have the advantages of working outside of IPython as well.
从
IPython 5.0
and开始,matplotlib 2.0
您可以避免使用 IPython 的特定魔法,而使用matplotlib.pyplot.ion()
/matplotlib.pyplot.ioff()
也具有在 IPython 之外工作的优点。
回答by Ashiq Imran
Provided you are running Jupyter Notebook, the %matplotlib inline command will make your plot outputs appear in the notebook, also can be stored.
如果您正在运行 Jupyter Notebook,%matplotlib 内联命令将使您的绘图输出显示在笔记本中,也可以存储。
回答by user9196865
It is not mandatory to write that. It worked fine for me without (%matplotlib
) magic function.
I am using Sypder compiler, one that comes with in Anaconda.
写那个不是强制性的。没有 ( %matplotlib
) 魔法功能对我来说效果很好。我正在使用 Sypder 编译器,这是 Anaconda 附带的编译器。