18

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.

  • 4
    I currently use Jupyter Lab 2.2.6, but previously used Jupyter Notebook. I never use `%matplotlib inline`, and I haven't used it for at least 2 years. – Trenton McKinney Jan 29 '21 at 02:49
  • @TrentonMcKinney, I currently don't use %matplotlib inline in Jupyter Notebook. This works for me. But I want to have a solid correct answer to share with others. So many resources out there recommend using it. However, I feel it may be outdated. But I can't tell so for sure unless I find a official documentation saying that. – Zolzaya Luvsandorj Jan 29 '21 at 03:02
  • 1
    I think this question is going to lead to _opinion based_ answers. – Trenton McKinney Jan 29 '21 at 03:16
  • 1
    Me too I haven't used `%matplotlib inline` in a very long time. Seems to work fine without in jupyter notebook and lab. – Stan Feb 17 '21 at 20:50
  • 1
    It is worth noting that `%matplotlib inline` has the drawback of conflicting with any `rcParams` modifications you make within the same cell (which is common if you like to put all setup in the first cell). https://github.com/jupyter/notebook/issues/3385 For this reason, I have stopped using it entirely. – wkzhu Jan 25 '22 at 14:34

2 Answers2

7

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.

neves
  • 33,186
  • 27
  • 159
  • 192
  • 1
    JupyterLite seems to behave differently. (1) It does not show a figure with `import matplotlib.pyplot as plt` without `plt.show()`. (2) It shows a figure with `%matplotlib inline` without `put.show()`. – T_T Dec 02 '22 at 06:35
3

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.

Arvind Raghu
  • 408
  • 3
  • 9
  • 12
    (1) _it is still convention_: maybe, (2) _keep your code clean and call on the plot that you made_: using it doesn't keep the code clean if it's not necessary, and it's not necessary to use `%matplotlib inline` to show plots, with or without `plt.show()` (in current versions), (3) _definitely recommended_: by whom? I don't recommend including any code that isn't explicitly required. – Trenton McKinney Jan 29 '21 at 02:50