4

I am just trying to understand why my jupyter installation does not require me to run '%matplotlib inline' (which, according to everything I've read, I should have to run that to get my plots inline in my jupyter notebook). But the fact is, my matplotlib plots appear inline in my notebook whether I run '%matplotlib inline' or not. I am running an anaconda install of jupyter on ubuntu under WSL, using chrome as the client to my notebook server. jupyter --version 4.4.0 and jupyter-notebook --version 5.7.8

I've checked my ~/.jupyter/jupyter_notebook_config.py file and see nothing in there about matplotlib.

If I run the following in my notbook:

import matplotlib
matplotlib.get_backend()

This is the output:

'module://ipykernel.pylab.backend_inline'

But if I run ipython directly in my WSL ubuntu window, and then run the above I get:

In [1]: import matplotlib
In [2]: matplotlib.get_backend()
Out[2]: 'agg'

Any idea how I can determine why this works this way? Is it perhaps part of the anaconda installation, that the kernal determines when it is running in a notebook and automatically switches the backend to 'backend_inline' ??

Thanks. Just curious why this is working this way for me.


RESPONDING TO COMMENTS:

@darthbith, each of these was done with a fresh restart of the kernel:

%matplotlib inline
import matplotlib
matplotlib.get_backend()
'module://ipykernel.pylab.backend_inline'

above is same as when I don't run %matplotlib inline

now restart kernel and try %matplotlib notebook:

%matplotlib notebook
import matplotlib
matplotlib.get_backend()
'nbAgg'
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Daniel Goldfarb
  • 6,937
  • 5
  • 29
  • 61
  • 1
    I don't think this is special to Anaconda, I think this is built in to matplotlib. For a long time, Matplotlib has detected which backends are available for a particular interpreter to try to pick the most appropriate one. Just out of curiosity, which backend do you get if you do `%matplotlib inline` or `%matplotlib notebook` before you `import matplotlib`? – darthbith Jul 18 '19 at 00:53
  • 1
    Na na, matplotlib will **never** select an external backend (in this case `ipykernel.pylab.backend_inline` by itself). This is clearly something jupyter/IPython is doing. – ImportanceOfBeingErnest Jul 18 '19 at 01:06
  • To @darthbith -- I have responded to your curiousty in an edit to the question (because I thought i could not format code in a comment) – Daniel Goldfarb Jul 18 '19 at 01:47
  • 2
    So it looks like [it's hardcoded](https://github.com/ipython/ipykernel/blob/7267f5da77300ab6a46e95f00be0eaddbe9c389a/ipykernel/kernelapp.py#L402) in ipykernel, which [gets called](https://github.com/ipython/ipykernel/blob/7267f5da77300ab6a46e95f00be0eaddbe9c389a/ipykernel/kernelapp.py#L489) upon initialization. But this may only be half the truth because in principle `IPython` has a [`InteractiveShellApp.matplotlib`](https://ipython.readthedocs.io/en/stable/config/options/kernel.html#configtrait-InteractiveShellApp.matplotlib) configuration parameter. – ImportanceOfBeingErnest Jul 18 '19 at 02:36
  • @ImportanceOfBeingErnest -- Thanks! – Daniel Goldfarb Jul 18 '19 at 18:21

0 Answers0