SMAmir
SMAmir

Reputation: 75

%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". But, I don't see any difference in my plot results with or without the use of "%matplotlib inline". Can someone explain this to me if I am misunderstanding something? Here's a simple code I have tried:

Graph with %matplotlib inline

%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. What is the point of using or not using the "%matplotlib inline" function then?

Upvotes: 4

Views: 3262

Answers (2)

SMAmir
SMAmir

Reputation: 75

Thanks to @ImportanceOfBeingErnest, figured that the problem comes from the notebook backend already set to inline. Once you run the code matplotlib.get_backend(), you can see the backend within the notebook is already set to inline by default. I am guessing this comes as a default now in Anaconda's version of Python 3.7's notebooks.

Upvotes: 3

J Edward Hammond
J Edward Hammond

Reputation: 527

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. 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. This performs the necessary behind-the-scenes setup for IPython to work correctly hand in hand with matplotlib; 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.

If you change your command to %matplotlib notebook for example, the result should change. this command is to make sure it is set to inline if it is not already.

Upvotes: 0

Related Questions