Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is %matplotlib inline still needed?

There are many resources that explains why %matplotlib inline is necessary to display plots inline. E.g. Purpose of %matplotlib inline. However, I feel that it is no longer necessary if we are using later version of IPython in your Jupyter Notebook. This is because I can display plots inline with or without running %matplotlib inline (each time I restart my kernel, IPython version I am using is 7.17.0). My hunch is that perhaps inline backend is activated by default for recent versions.

When I run %matplotlib to check the current backend on a new session, it says Qt5Agg. After running %matplotlib inline, when I check again by running %matplotlib, it displays the same Qt5Agg. This makes me think that %matplotlib inline is redundant as it's not changing anything. I haven't changed any IPython config myself btw.

However, I don't see any official documentation saying that inline backend is activated by default for IPython versions x.x.x+. I found this and this Github issue that was close to what I was trying to find but it doesn't fully confirm "You no longer need to run %matplotlib inline if your IPython version is x.x.x+ as it is the default behaviour". I looked through IPython recent release notes, but doesn't seem to confirm the hypothesis.

Is my hunch right? If so, what IPython versions it is not required? Is there any official documentation saying that? If not, how come I am able to plot inline without running %matplotlib inline?

This may seem as a possible duplication of Why don't I need “%matplotlib inline” in my jupyter notebook?. I wasn't able to confirm my hunch from this thread.

like image 394
Zolzaya Luvsandorj Avatar asked Jan 28 '21 09:01

Zolzaya Luvsandorj


People also ask

Is %Matplotlib Inline still necessary?

So %matplotlib inline is only necessary to register this function so that it displays in the output. Running import matplotlib. pyplot as plt also registers this same function, so as of now it's not necessary to even use %matplotlib inline if you use pyplot or a library that imports pyplot like pandas or seaborn.

What happens if I dont use %Matplotlib inline?

In the current versions of the IPython notebook and jupyter notebook, it is not necessary to use the %matplotlib inline function. As, whether you call matplotlib. pyplot. show() function or not, the graph output will be displayed in any case.

Is matplotlib inline by default?

Matplotlib BackendsThese plots are by default, displayed inline, which means, they're displayed in the notebook itself. However, you can also display the plot outside of the notebook, which can be done by changing the Matplotlib backend.

What means %Matplotlib inline?

%matplotlib inline sets the backend of matplotlib to the 'inline' backend: 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.


Video Answer


2 Answers

The answer is basically no.

There's a fine bug report that explains why.

The only ones who would need to do it are users of the non-object-oriented interface of matplotlib. Users who do not use pyplot.

If you import pyplot, or even if you import pandas, then it isn't necessary to execute %matplotlib inline.

like image 53
neves Avatar answered Oct 17 '22 09:10

neves


The only reason %matplotlib inline is used is to render any matplotlib diagrams even if the plt.show() function is not called.

However, even if %matplotlib inline is not used, Jupyter will still display the Matplotlib diagram as an object, with something like matplotlib.lines.Line2D object at 0x0392A9D0 appearing before it in the console.

The end point is that it is not necessary anymore, however, it is still convention to keep your code clean and call on the plot that you made, and definitely recommended.

like image 42
Arvind Raghu Avatar answered Oct 17 '22 09:10

Arvind Raghu