“%matplotlib内联”的目的

本文翻译自:Purpose of “%matplotlib inline”

有人可以向我解释%matplotlib inline确切用途是什么?


#1楼

参考:https://stackoom.com/question/2uXXg/matplotlib内联-的目的


#2楼

%matplotlib is a magic function in IPython. %matplotlib是IPython中的魔术函数 I'll quote the relevant documentation here for you to read for convenience: 为了方便起见,我在这里引用相关文档供您阅读:

IPython has a set of predefined 'magic functions' that you can call with a command line style syntax. IPython有一组预定义的“魔术函数”,您可以使用命令行样式的语法来调用它们。 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. 换行符以%字符作为前缀,其工作方式与OS命令行调用非常相似:它们作为行的其余部分作为参数,其中的参数传递时不带括号或引号。 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. 单元魔术的前缀为%%%,并且它们是函数,它们不仅作为该行的其余部分作为参数,而且还作为单独的参数作为其下方的行的参数。

%matplotlib inline sets the backend of matplotlib to the 'inline' backend : %matplotlib inline %matplotlib inline 的后端设置为'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. 有了这个后端,绘图命令的输出就在Jupyter笔记本之类的前端内联显示,就在生成它的代码单元正下方。 The resulting plots will then also be stored in the notebook document. 然后,生成的图也将存储在笔记本文档中。

When using the 'inline' backend, your matplotlib graphs will be included in your notebook, next to the code. 使用“内联”后端时,您的matplotlib图形将包含在笔记本中代码旁边。 It may be worth also reading How to make IPython notebook matplotlib plot inline for reference on how to use it in your code. 还可能值得阅读如何内联制作IPython笔记本matplotlib绘图,以获取有关如何在代码中使用它的参考。

If you want interactivity as well, you can use the nbagg backend with %matplotlib notebook (in IPython 3.x), as described here . 如果你想交互,以及,你可以使用nbagg后端%matplotlib notebook (在IPython中3.X),如所描述这里


#3楼

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 . 要进行此设置,必须先执行%matplotlib magic command ,然后再执行matplotlib任何绘制或导入。 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. 有了这个后端,绘图命令的输出就在Jupyter笔记本之类的前端内联显示,就在生成它的代码单元正下方。 The resulting plots will then also be stored in the notebook document. 然后,生成的图也将存储在笔记本文档中。


#4楼

It is not mandatory to write that. 不必写那个。 It worked fine for me without ( %matplotlib ) magic function. 没有( %matplotlib )魔术功能,它对我来说效果很好。 I am using Sypder compiler, one that comes with in Anaconda. 我正在使用Sypder编译器,这是Anaconda随附的。


#5楼

If you want to add plots to your Jupyter notebook, then %matplotlib inline is a standard solution. 如果要将图添加到Jupyter笔记本中,则%matplotlib inline是标准解决方案。 And there are other magic commands will use matplotlib interactively within Jupyter. 还有其他魔术命令将在Jupyter中交互使用matplotlib

%matplotlib : any plt plot command will now cause a figure window to open, and further commands can be run to update the plot. %matplotlib :任何plt plot命令现在都将导致图形窗口打开,并且可以运行其他命令来更新图形。 Some changes will not draw automatically, to force an update, use plt.draw() 使用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 :仅在笔记本中绘制静态图像


#6楼

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. 有些人从python shell交互地使用matplotlib,并且在键入命令时弹出绘图窗口。 Some people run Jupyter notebooks and draw inline plots for quick data analysis. 有些人运行Jupyter笔记本并绘制内联图以进行快速数据分析。 Others embed matplotlib into graphical user interfaces like wxpython or pygtk to build rich applications. 其他人则将matplotlib嵌入到wxpython或pygtk等图形用户界面中,以构建丰富的应用程序。 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. 有些人在批处理脚本中使用matplotlib从数字模拟生成后记图像,还有一些人运行Web应用程序服务器以动态提供图形。 To support all of these use cases, matplotlib can target different outputs, and each of these capabilities is called a backend; 为了支持所有这些用例,matplotlib可以针对不同的输出,这些功能中的每一个都称为后端。 the "frontend" is the user facing code, ie, the plotting code, whereas the "backend" does all the hard work behind-the-scenes to make the figure. “前端”是用户面对的代码,即绘图代码,而“后端”则是幕后的所有艰苦工作,以制作图形。

So when you type %matplotlib inline , it activates the inline backend. 因此,当您键入%matplotlib inline时,它将激活内联后端。 As discussed in the previous posts : 如前几篇文章所述:

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. 有了这个后端,绘图命令的输出就在Jupyter笔记本之类的前端内联显示,就在生成它的代码单元正下方。 The resulting plots will then also be stored in the notebook document. 然后,生成的图也将存储在笔记本文档中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值