Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

%matplotlib notebook showing a blank histogram

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?

like image 258
João Abrantes Avatar asked Dec 13 '16 16:12

João Abrantes


People also ask

Why is my matplotlib graph blank?

The reason your plot is blank is that matplotlib didn't auto-adjust the axis according to the range of your patches. Usually, it will do the auto-adjust jobs with some main plot functions, such as plt. plot(), plt.

What happens if I dont use %Matplotlib inline?

In the current versions of the IPython notebook and jupyter notebook, it is not necessary to use the %matplotlib inline function. As, whether you call matplotlib. pyplot. show() function or not, the graph output will be displayed in any case.

Why is matplotlib not showing plot?

The issue actually stems from the matplotlib backend not being properly set, or from a missing dependency when compiling and installing matplotlib.

What can I use instead of %Matplotlib inline?

show() . If you do not want to use inline plotting, just use %matplotlib instead of %matplotlib inline .


3 Answers

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

like image 182
ImportanceOfBeingErnest Avatar answered Oct 23 '22 04:10

ImportanceOfBeingErnest


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.

like image 22
Anderas Avatar answered Oct 23 '22 04:10

Anderas


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

like image 1
Alex Avatar answered Oct 23 '22 04:10

Alex