3

Based on my understanding from this question %matplotlib inline is used so figures can be shown in Jupyter. But figures are shown in Jupyter without using %matplotlib inline perfectly fine. for example the following code:

import numpy as np
import matplotlib.pyplot as plt

plt.plot(np.random.randn(50).cumsum())
plt.show()

So is %matplotlib inline obsolete or am I misunderstanding it's purpose?

Mathemagician
  • 431
  • 4
  • 13
  • 2
    Till date there seems to be no answer to this mystery. Check this unanswered question of mine from over an year ago: [(https://stackoverflow.com/questions/54329901/behavior-of-matplotlib-inline-plots-in-jupyter-notebook-based-on-the-cell-content](https://stackoverflow.com/questions/54329901/behavior-of-matplotlib-inline-plots-in-jupyter-notebook-based-on-the-cell-conten) – Sheldore May 29 '20 at 07:45
  • 1
    I'm not sure I would call it obsolete, but rather the default. If you run `print(plt.get_backend())` you'll see it is `ipykernel.pylab.backend_inline` right from the start. If you switch to some other backend you can use that magic to switch back. – jayveesea May 29 '20 at 11:06
  • @jayveesea I think you should submit your comment as an answer as it answers the question. – Mathemagician May 29 '20 at 12:28

1 Answers1

1

The default matplotlib backend in a jupyter notebook is inline. You can inspect this by using print(plt.get_backend()) after loading matplotlib. For example:

import matplotlib.pyplot as plt
print(plt.get_backend())

returns module://ipykernel.pylab.backend_inline

The magic %matplotlib can be used to switch back to inline if you had switched to some other backend. The following cells can illustrate this when run in a notebook.

In [100]:

import matplotlib.pyplot as plt
plt.plot(np.random.randn(50).cumsum())

In [101]:

%matplotlib notebook
plt.plot(np.random.randn(50).cumsum())

In [103]:

%matplotlib inline
plt.plot(np.random.randn(50).cumsum())
jayveesea
  • 2,886
  • 13
  • 25