3

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 ?

3
  • How are you running the code? Using python or ipython? I would suggest you to use jupyter notebook. Commented Dec 25, 2016 at 7:41
  • I'm running the code in command line on Ubuntu 16.04 Commented Dec 25, 2016 at 7:48
  • 1
    %matplotlib inline is used to render plots inline in ipython notebooks. If you are running this as a script from command line, try with this line commented. Commented Dec 25, 2016 at 8:00

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.