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

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

Related

How to change the default backend in matplotlib from 'QtAgg' to 'Qt5Agg' in Pycharm?

Qt5Agg is necessary to use the mayavi 3D visualization package. I have installed PyQt5 and mayavi using pip in a separate copied conda environment. The default backend then changes from TkAgg to QtAgg. This is a bit weird because in an earlier installation in a different PC the default changed directly to Qt5Agg. I always check the backend using the following commands from the python console :
import matplotlib
matplotlib.get_backend()
Even with the backend being 'QtAgg', I am able to use mayavi from the terminal without any issue but not when I do so in Pycharm. Here I get a non-responsive empty window (image below) :
Image of the non-responsive window
I have been able to get rid of this issue by explicitly using Qt5Agg instead of QtAgg before the plt call :
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
But I would prefer a better way than using the above in every script that I write. As I had mentioned earlier, I already have mayavi installed and have used it successfully it in Pycharm in a different PC and there the default backend is 'Qt5Agg' and hence there is no need to change the backend explicitly.
Is there anything obvious that I'm overlooking ? Can you please let me know of a way to change the default backend for matplotlib from QtAgg to Qt5Agg after PyQt5 installation using pip ?
Thanks in advance !!
Thanks to #PaulH's comment, I was able to solve the issue. Owing to #mx0's suggestion, I shall now explicitly mention the fix below so that others can also benefit from it.
In a particular conda environment, if matplotlib package is installed, then there will be a 'matplotlibrc' file stored somewhere that defines what the default backend will be whenever matplotlib is imported from that conda environment. The location of this 'matplotlibrc' can be found using the following commands :
import matplotlib
matplotlib.matplotlib_fname()
Please look into the following link if there's any deprecation issue with the above commands :
https://matplotlib.org/stable/tutorials/introductory/customizing.html#customizing-with-matplotlibrc-files
Once the location of the 'matplotlibrc' file is known, open it and simply uncomment one line inside this file. Just change the backend from :
##backend: Agg
to :
backend: Qt5Agg
And that's it. All the plot window troubles in PyCharm will be solved as far as the mayavi 3D visualization package is concerned. For any other use, where a specific backend is necessary, you can also set the default to any other backend of choice.

How can I display an image on VS Code and PyCharm?

I want to choose the best IDE for Python programming so I'm testing different softwares but I have problem.
When I try this code blocks on VS Code I don't see any error but the image is not showing
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread('exit-ramp.jpg')
plt.imshow(image)
VS Code
When I try run the same code on PyCharm I see some errors
PyCharm
But when I run the same code on Jupyter Notebook it works. What can I do?
Bro, try that:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread('exit-ramp.jpg')
plt.imshow(image)
plt.show()
i've made a test in my vscode.
Few weeks ago i used a lot pycharm... but i get some library errors with pandas and pycharm its a little bit heavy.
VSCODE its light and i didnt get any library error that i had with pycharm.
I believe this is a backend problem to be changed in matplotlib. If you start your Python shell whether you are in a bash shell or in vscode, you should use a backend that will show the plot. Otherwise the plt.show() call is required.
So, one solution is to change the backend either manually in you Python shell or when starting the Python shell as explained here below.
Note, however, that I always use the qt5 backend but got the same issue as yours: no plot appears. I changed to the Qt5Agg backend and it worked for me (providing PyQt5 is installed).
So, first solution, you can try to set the backend when starting the Python shell:
ipython --pylab Qt5Agg
You should use a GUI backend amongst the possible backend. If you do not known, try to replace Qt5Agg by auto
The second solution consists in changing the backend manually once in the Python shell:
import matplotlib as plt
plt.use('Qt5Agg')
Right click on your code -> Run current file in interactive window.

Pycharm Jupyter Interactive Matplotlib

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

%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 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

Categories