25

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.

5
  • 5
    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. Commented Jan 29, 2021 at 2: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. Commented Jan 29, 2021 at 3:02
  • 1
    I think this question is going to lead to opinion based answers. Commented Jan 29, 2021 at 3: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. Commented Feb 17, 2021 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). github.com/jupyter/notebook/issues/3385 For this reason, I have stopped using it entirely. Commented Jan 25, 2022 at 14:34

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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().
7

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.

1 Comment

(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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.