“%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. 然後,生成的圖也將存儲在筆記本文檔中。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章