%matplotlib notebook showing a blank histogram - python

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:
If I use %matplotlib inline everything works fine:
What's going on?

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

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.

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

The issue seems to be an interaction between switching:
%matplotlib notebook
%matplotlib inline
and using the figure "power button" on interactive plots:
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

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!

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.

Related

Not possible to see the plot created in Jupyter notebooks when using magic: %matplotlib notebook. Windows 10 and Chrome

Hi everyone. I am running jupyter notebook through Chrome and Windows
10. the versions are : Windows 10 Chrome Version 84.0.4147.105 (Official Build) (64-bit) I am using the magic %matplotlib notebook.
To be able to zoom in and out in the plot. The code works in other
machines with same windows and version. However, when I run it in my
personal laptop, the plot is not possible to see it:
I was trying to follow the following info found: help
I have run out of ideas, I restarted the laptop, reinstalled jupyter,
and nothing seems to work. Did anyone have a similar issue? Here is the code:
%matplotlib notebook
import matplotlib.pyplot as plt
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
import numpy as np
import pandas as pd
def g(qi,d):
fig = plt.figure()
x_1 = df['dprod_year']
y_1 = qi*np.exp(-x_1*d)
plt.scatter(x_1,y_1,marker='+')
plt.scatter(df['dprod_year'],df['qoil_1000b'],marker='o',color='grey')
plt.ylim(0,4000)
plt.xlim(0,60)
plt.grid(True,axis='both')
"""
#interact(g,qi=(0.0000,5000,10),d=(0.0000,0.02000,0.0010))
"""
interact(g,qi=widgets.FloatSlider(value=3900,min=0,max=4000,step=10,description='qi:',readout_format='.1f'),
d=widgets.FloatSlider(value=0.0061,min=0.0001,max=0.01,step=0.001,description='d:',readout_format='.5f'))
plt.show()
Thank you very much for your help.
You should try to change from %matplotlib notebook to %matplotlib inline.
If that doesn't work then try to restart the kernel.
It seems that in some cases it helps to repeat the setting of the notebook backend, i.e. try calling it twice like
%matplotlib notebook
%matplotlib notebook
An analysis for why that is can be found in this comment

%matplotlib inline versus %matplotlib notebook display problems in pythonAnywhere jupyter

This is not a duplicate as suggested. I am not trying to switch backends. I am just trying to use the %matplotlib notebook formulation. Moreover, the solutions suggested in that other question (place the%matplotlib notebook before the the from matplotlib import pylplot as plt OR trying call the magic command twice in a row) do not work for me. The behavior (blank notebook charts) remains.
place the%matplotlib notebook before the the from matplotlib import pylplot as plt OR trying call the magic command twice in a row
When I use %matplotlib inline my charts always show, but they're generally on the small side, and have no ability to zoom/pan.
When I use %matplotlib notebook I get zoomable, pannable charts, but I sometimes encounter problems where the charts display as blank.
Background:
PythonAnywhere notebook 3.6 Python. Using update Google Chrome from Windows 7.
If I reopen my existing notebook, even restart the kernel and re-run all the cells, all my .plot() appear blank. But if I contemporaneously start a brand new 3.6 notebook, %matplotlib notebook plots work fine. Killing all the jupyter-related processes doesn't help either.
This does not always occur. But if one chart appears blank, they all appear blank.
I would like to use the notebook magic, but this problem is pretty profound.
I have found some stability on using %matplotlib notebook, after including plt.close(), before all my plots.
A pretty dirty solution, I know, but it's working. Here Windows 10, Python 3.6.2 and Jupyter 6.03 (updated Jupyter haven't solved the issue).
Hope this helps.
%matplotlib qt will do your job.

%matplotlib inline doesn't work on iPython and Jupyter console

I'm trying out Jupyter console for the first time, but can't get the %matplotlib inline magic to work. Below is a screenshot of an example session:
The plot shows in a separate window after I run Line 6, and Line 7 doesn't do anything.
When I run %matplotlib --list, inline is given as one of the options:
Available matplotlib backends: ['osx', 'qt4', 'qt5', 'gtk3', 'notebook', 'wx', 'qt',
'nbagg', 'agg', 'gtk', 'tk', 'ipympl', 'inline']
When I try to use another backend, say qt5, it gives an error message because I don't have any Qt installed.
ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5, or PySide
package to be installed, but it was not found.
Running %matplotlib?? reads:
If you are using the inline matplotlib backend in the IPython Notebook
you can set which figure formats are enabled using the following::
In [1]: from IPython.display import set_matplotlib_formats
In [2]: set_matplotlib_formats('pdf', 'svg')
The default for inline figures sets `bbox_inches` to 'tight'. This can
cause discrepancies between the displayed image and the identical
image created using `savefig`. This behavior can be disabled using the
`%config` magic::
In [3]: %config InlineBackend.print_figure_kwargs = {'bbox_inches':None}
But I don't know if it's something I can tweak around to solve my issue.
When I try it the magic IPython console, it says inline is an Unknown Backend.
UnknownBackend: No event loop integration for u'inline'. Supported event loops are: qt,
qt4, qt5, gtk, gtk2, gtk3, tk, wx, pyglet, glut, osx
I've also found this issue on github after some googling but I don't even know if it's relevant to my situation (most of their conversation didn't make sense to me lol).
Lastly, I'm not sure if this issue is related at all, but here it is, just in case: when I try to open Vim in Jupyter via the !vim command, it glitches pretty badly, preventing me from even exiting out of Jupyter itself without closing the terminal altogther. Vim works perfectly fine when called inside IPython console, however.
I'm using matplotlib 2.0.0.
If anyone could help me figure this out, that'd be great! Thank you!
You’re running a console which is completely text based and incapable of showing images. Therefore, although inline is available, it's not producing inline output.
I'm not sure why it doesn't throw an error, though, which it does in my case:
You can use %matplotlib inline in a GUI console, like Jupyter QTConsole
or in a jupyter notebook in the browser
%matplotlib without the inline works for me (I'm using osx and IPython 7.0.1)
If you're running an old version of ipython try %pylab inline instead. See notes in this tutorial

matplotlib inline syntax error

I'm testing a python program, which contains calling for IPython. But I got errors in the following code:
If I use
%matplotlib inline
I got a syntax error at the "%" symbol.
I found a solution to this problem using:
from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')
I got error saying:
AttributeError: 'NoneType' object has no attribute 'run_line_magic'
I'm using Ubuntu 16.04 and running the code via command line. How can I fix this ?
This %matplotlib magic is used to display graphs (of matplotlib.pyplot objects). This needs UI to display. So cannot be display on command prompt.
According to IPython documentation,
If the %matplotlib magic is called without an argument, the output of a plotting command is displayed using the default matplotlib backend in a separate window. Alternatively, the backend can be explicitly requested using, for example:
%matplotlib gtk
A particularly interesting backend, provided by IPython, is the inline backend. This is available only for the Jupyter Notebook and the Jupyter QtConsole. It can be invoked as follows:
%matplotlib inline
Simple solution would be to replace %matplotlib inline with %matplotlib and run it using ipython.
Alternatively, what you could do is download jupyter notebook and run that code there.
Or as #tihom said in comments, you could comment or remove that line and run the code but this wouldn't display the graphs and other things.
You can use pylab mode in ipython. Simply run ipython --pylab in the terminal. That will launch ipython configured to be able to support the matplotlib GUI backend

Jupyter Notebook ImportError, %matplotlib qt

I am using
%matplotlib inline
to display plots inside the notebook. I would like to disable this for several cells. So, I try
%matplotlib qt
This outputs the following error:
ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5,
or PySide package to be installed, but it was not found.
I am not sure how to solve this, as everything is up to date.
How can I solve the above?
Is there another way to disable %matplotlib inline in a certain cell without restarting the entire kernel?
You might be able to use plt.switch_backend, although as the documentation states, this is an experimental feature. The following works for me, using matplotlib 1.5 and an IPython 4.0.1:
In [1]: from matplotlib import pyplot as plt
In [2]: import numpy as np
# plot appears inline (default)
In [3]:plt.plot(np.random.randn(10))
Out[3]:[<matplotlib.lines.Line2D at 0x7fac4408e390>]
In [4]: plt.switch_backend('QtAgg4')
# plot appears inside a separate Qt4 window
In [5]:plt.plot(np.random.randn(10))
Out[5]:[<matplotlib.lines.Line2D at 0x7fac3b408a90>]
You might need to change 'QtAgg4' according to whichever version of PyQt you have installed - this could be the cause of the error you mentioned in the question. Another interactive backend that should work on Mac would be 'CocoaAgg'. If the images are very large you could also use the 'Agg' backend to suppress plotting altogether, and instead save the resulting figure(s) straight to disk.
If you don't have a specific backend installed use "agg":
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
Reference: https://github.com/matplotlib/matplotlib/issues/9017

Categories