45

In my Jupyter notebook I am now using %matplotlib notebook instead of %matplotlib inline, it's awesome that I can now interact with my plots on Jupyter. However, when I try to make an histogram I get a blank plot:

%matplotlib notebook

If I use %matplotlib inline everything works fine: %matplotlib inline

What's going on?

João Abrantes
  • 4,772
  • 4
  • 35
  • 71
  • If you right-click where it should be and Inspect the page, is there a `
    ` there? Are there any Javascript errors?
    – Nick T Dec 13 '16 at 16:18
  • @NickT I don't get the usual `
    ` that I get with other successful plots. I get no Javascript errors.
    – João Abrantes Dec 13 '16 at 16:24
  • 1
    I cannot reproduce this, for me it's working fine on python 2.7, jupyter server 4.0.6, IPython 4.0. (I tried it in under Windows using Firefox 50 and Opera 41). Maybe it's still worthwhile to produce a [MCVE], such that people are testing the same code. – ImportanceOfBeingErnest Dec 13 '16 at 18:06
  • 1
    I see a bit clearer. I think it has something to do with changing on the fly between `%matplotlib notebook` and `%matplotlib inline`. Try to create a new notebook and use it there. – ImportanceOfBeingErnest Dec 13 '16 at 18:17
  • I had the same problem, @ImportanceOfBeingErnest 's reply did the trick for me - I was switching from %matplotlib inline to %matplotlib notebook and getting the same blank plot - then I restarted the notebook and used notebook from the get go, and the plots appear correctly. – jjs Dec 20 '16 at 20:22

6 Answers6

83

Seeing that my comment above has indeed helped someone to solve the problem I will post it as an answer.

The problem occurs if you switch from %matplotlib inline to %matplotlib notebook without restarting the kernel.

Switching from %matplotlib notebook to %matplotlib inline works fine.

So the solution is to either restart the kernel or start a new notebook.

It seems that in some cases it helps to repeat the setting of the notebook backend, i.e. call it twice like

%matplotlib notebook
%matplotlib notebook

An analysis for why that is can be found in this comment

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
12

The answer is not necessarily to restart the entire kernel.

If you reload the matplotlib module, it will work, too. Provided you use Python 3.6 like me, and you have import matplotlib.pyplot as plt like me:

from importlib import reload
reload(plt)
%matplotlib notebook

It does the trick. Yes it is still a hack. At least this is an independent codecell you can use in the middle of the notebook. Switching back via %matplotlib inline is not a problem.

You can also remove once imported names from the sys.modules list, then they get imported again when you call the import again.

import sys
sys.modules.pop('matplotlib')
from matplotlib import pyplot as plt

In many cases, that's a less good idea. But it might sometimes be the only straw to hold on.

Anderas
  • 630
  • 9
  • 20
1

I was able to fix it by downgrading matplotlib to 3.1.3:

conda install matplotlib=3.1.3

I was running version 3.3.2 and had this same issue. I was not switching between %matplotlib inline and %matplotlib notebook, and it did not matter if I placed %matplotlib notebook before or after importing

Alex
  • 1,064
  • 1
  • 11
  • 16
0

The issue seems to be an interaction between switching:

%matplotlib notebook
%matplotlib inline

and using the figure "power button" on interactive plots:

enter image description here

Solution: If you pressed the button and are getting blank plots, restart Jupyter and don't use the power button again if you need to switch between notebook and inline

Justas
  • 5,718
  • 2
  • 34
  • 36
0

On a new notebook if you start by using %matplotlib notebook then you can switch between that an inline without issue. I confirm that both restarting of the kernel and the running of the code do the trick!

Periklisk
  • 1
  • 3
0

For me, it also appeared after switching from 'inline' to 'notebook' but restarting the kernel didn't work. I had to go through file > close and halt, and then either reopen it or restart the kernel if the page is still open.

Boson Bear
  • 103
  • 1
  • 1
  • 3