Zolzaya Luvsandorj
Zolzaya Luvsandorj

Reputation: 635

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.

Upvotes: 25

Views: 11695

Answers (2)

neves
neves

Reputation: 39433

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 with the standard import matplotlib.pyplot as plt, or even if you import pandas, then it isn't necessary to execute %matplotlib inline.

Upvotes: 10

Arvind Raghu
Arvind Raghu

Reputation: 458

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.

Upvotes: 7

Related Questions