Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

%matplotlib line magic causes SyntaxError in Python script

I try to run the following codes on Spyder (Python 2.7.11):

# -*- coding: utf-8 -*-  import numpy as np import pandas as pd  %matplotlib inline  import matplotlib.pyplot as plt import matplotlib.cm as cm  import tensorflow as tf  # settings LEARNING_RATE = 1e-4 # set to 20000 on local environment to get 0.99 accuracy TRAINING_ITERATIONS = 2000          DROPOUT = 0.5 BATCH_SIZE = 50  # set to 0 to train on all available data VALIDATION_SIZE = 2000  # image number to output IMAGE_TO_DISPLAY = 10 

But I got this error:

line 10     %matplotlib inline     ^ SyntaxError: invalid syntax. 

I appreciate if anybody gives me an explanation.

P.S. the code is from Kaggle competition project: Digit Recognizer

like image 882
John Avatar asked Feb 24 '16 07:02

John


People also ask

What does %Matplotlib mean in Python?

What Does Matplotlib Mean? Matplotlib is a plotting library available for the Python programming language as a component of NumPy, a big data numerical handling resource. Matplotlib uses an object oriented API to embed plots in Python applications.

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.

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 .

Do We Still Need %Matplotlib inline?

So %matplotlib inline is only necessary to register this function so that it displays in the output. Running import matplotlib. pyplot as plt also registers this same function, so as of now it's not necessary to even use %matplotlib inline if you use pyplot or a library that imports pyplot like pandas or seaborn.


2 Answers

Line magics are only supported by the IPython command line. They cannot simply be used inside a script, because %something is not correct Python syntax.

If you want to do this from a script you have to get access to the IPython API and then call the run_line_magic function.

Instead of %matplotlib inline, you will have to do something like this in your script:

from IPython import get_ipython get_ipython().run_line_magic('matplotlib', 'inline') 

A similar approach is described in this answer, but it uses the deprecated magic function.

Note that the script still needs to run in IPython. Under vanilla Python the get_ipython function returns None and get_ipython().run_line_magic will raise an AttributeError.

like image 144
MB-F Avatar answered Oct 01 '22 08:10

MB-F


Because line magics are only supported by the IPython command line not by Python cl, use: 'exec(%matplotlib inline)' instead of %matplotlib inline

like image 36
Abraham Sanchez Avatar answered Oct 01 '22 10:10

Abraham Sanchez