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.
Related
Interactive matplotlib plotting is already a thing, but does not work properly in Pycharm, when used within a jupyter notebook.
The %matplotlib notebook does not work (throws no error, but I get <IPython ... JavaScript object> instead of a plot. If I plot normally (also with or without plt.show()) I just get a png and cannot interact in any way (even if, e.g., sliders are visible).
I couldn't find any answers elsewhere to this exact problem. It might be working in the browser version of jupyter, but I would like to stick to using PyCharm.
Pycharm v 2017.3 Community Edition
You can try:
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
Instead of importing only the peplos.
It's a trick I found on forum of Jetbrains
https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000736584-SciView-in-PyCharm-2017-3-reduces-functionality-of-Matplotlib It works for me. With this, you skip actually the Sciview and plot in a normal matplotlib window
I'm trying to come up to speed on the animation functionality in pyplot. I've grabbed the example code given here: https://matplotlib.org/2.0.0/examples/animation/animate_decay.html
and I'm attempting to run it myself. The result I get is empty axes with no animation at all. My setup is anaconda 4.3, with python 3.6. Matplotlib is version 2.0.0, and I'm running this in a jupyter notebook. I've tried IE 11, Chrome 59 and Firefox 54 on both a windows 7 machine and a Mac.
If you're using Spyder, you need to change your Matplotlib backend by going to the menu
Tools > Preferences > IPython console > Graphics
and then selecting Automatic in the section called Graphics backend. After that, you need to restart Spyder or the kernel associated with the console you want this change to take effect on.
A jupyter notebook would probably use the inline backend by default which cannot be animated (since it produces png images).
You may use the notebook backend to have the plot produced in an interactive notebook cell,
%matplotlib notebook
or the Tk backend to get a new window with the animation popping up,
%matplotlib tk
If using matplotlib 2.1, you may also use the %matplotlib inline backend and show the animation as JavaScript,
from IPython.display import HTML
HTML(ani.to_jshtml())
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
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
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.