Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python SyntaxError: invalid syntax %matplotlib inline

I got this error in my python script:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

from utils import progress_bar_downloader
import os
#Hosting files on my dropbox since downloading from google code is painful
#Original project hosting is here: https://code.google.com/p/hmm-speech-recognition/downloads/list
#Audio is included in the zip file
link = 'https://dl.dropboxusercontent.com/u/15378192/audio.tar.gz'
dlname = 'audio.tar.gz'

if not os.path.exists('./%s' % dlname):
    progress_bar_downloader(link, dlname)
    os.system('tar xzf %s' % dlname)
else:
    print('%s already downloaded!' % dlname)

I want use matplotlib but it gives syntax error, I tried sudo apt-get install python-matplotlib

like image 583
Prajakta Dumbre Avatar asked Sep 12 '16 11:09

Prajakta Dumbre


People also ask

Why is %Matplotlib inline?

Why matplotlib inline is used. You can use the magic function %matplotlib inline to enable the inline plotting, where the plots/graphs will be displayed just below the cell where your plotting commands are written. It provides interactivity with the backend in the frontends like the jupyter notebook.

Do I 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.

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.


2 Answers

if you are not using Jupyter IPython notebook, just comment out (or delete) the line, everything will work fine and a separate plot window will be opened if you are running your python script from the console.

However, if you are using Jupyter IPython notebook, the very first python code cell in your notebook should have the line "%matplotlib inline" for you to be able to view any plot.

like image 197
Sandipan Dey Avatar answered Sep 28 '22 00:09

Sandipan Dey


"%matplotlib inline" isn't valid python code, so you can't put it in a script.

I assume you're using a Jupyter notebook? If so, put it in the first cell and all should work.

like image 20
MMN Avatar answered Sep 27 '22 23:09

MMN