简体   繁体   English

%matplotlib inline 在 Jupyter Notebook 上不管有没有它都会给出相同的结果

[英]%matplotlib inline gives the same result with or without it on Jupyter Notebook

So I went through some questions being posted about the usage of %matplotlib inline function in Jupyter Notebook, I do understand that "%matplotlib inline sets the backend of matplotlib to the 'inline' backend" & "When using the 'inline' backend, your matplotlib graphs will be included in your notebook, next to the code".所以我经历了一些关于在 Jupyter Notebook 中使用 %matplotlib inline function 的问题,我明白“%matplotlib inline 将 matplotlib 的后端设置为'inline'后端”和“使用'inline'后端时”您的 matplotlib 图表将包含在您的笔记本中,位于代码旁边”。 But, I don't see any difference in my plot results with or without the use of "%matplotlib inline".但是,无论是否使用“%matplotlib inline”,我的 plot 结果都看不到任何区别。 Can someone explain this to me if I am misunderstanding something?如果我误解了什么,有人可以向我解释一下吗? Here's a simple code I have tried:这是我尝试过的简单代码:

Graph with %matplotlib inline使用 %matplotlib 内联的图形

%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt

plt.plot([[0,0],[1,1]], linewidth=4, label='random diagonal')

In the next code, I just took off the %matplotlib inline and it gives still gives me the same result.在下一个代码中,我刚刚取消了%matplotlib 内联,它仍然给了我相同的结果。 What is the point of using or not using the "%matplotlib inline" function then?那么使用或不使用“%matplotlib inline”function 有什么意义呢?

Thanks to @ImportanceOfBeingErnest, figured that the problem comes from the notebook backend already set to inline.感谢@ImportanceOfBeingErnest,发现问题来自已经设置为内联的笔记本后端。 Once you run the code matplotlib.get_backend() , you can see the backend within the notebook is already set to inline by default.运行代码matplotlib.get_backend()后,您可以看到笔记本中的后端已默认设置为内联。 I am guessing this comes as a default now in Anaconda's version of Python 3.7's notebooks.我猜这现在在 Anaconda 的 Python 3.7 笔记本中是默认设置的。

It's all about the backend running on the notebook.这都是关于在笔记本电脑上运行的后端。 You can use a couple different output modes such as printing it to the notebook, a new window, or inline.您可以使用几种不同的 output 模式,例如将其打印到笔记本电脑、新的 window 或内联模式。 A user here shows a couple of different ways it works according to the documentation :此处的用户根据文档显示了几种不同的工作方式:

To set this up, before any plotting or import of matplotlib is performed you must execute the %matplotlib magic command .要进行此设置,在执行matplotlib的任何绘图或导入之前,您必须执行%matplotlib magic command This performs the necessary behind-the-scenes setup for IPython to work correctly hand in hand with matplotlib ;这将执行必要的幕后设置,以使 IPython 与matplotlib一起正常工作; it does not, however, actually execute any Python import commands, that is, no names are added to the namespace.但是,它实际上并没有执行任何 Python 导入命令,也就是说,没有将名称添加到命名空间。

A particularly interesting backend, provided by IPython, is the inline backend. IPython 提供的一个特别有趣的后端是inline后端。 This is available only for the Jupyter Notebook and the Jupyter QtConsole.这仅适用于 Jupyter Notebook 和 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.有了这个后端,绘图命令的 output 将在 Jupyter 笔记本等前端内联显示,直接在生成它的代码单元下方。 The resulting plots will then also be stored in the notebook document.然后,生成的图也将存储在笔记本文档中。

If you change your command to %matplotlib notebook for example, the result should change.例如,如果您将命令更改为%matplotlib notebook ,结果应该会改变。 this command is to make sure it is set to inline if it is not already.此命令是为了确保它已设置为内联(如果尚未设置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM