python syntax error in matplotlib - python

I am using python 3.5, pycharm and getting invalid syntax at %matplotlib inline as output here is my code:
import time
import requests
import cv2
import operator
import numpy as np
# Import library to display results
import matplotlib.pyplot as plt
%matplotlib inline

I think this link may be what you are looking for: How to make IPython notebook matplotlib plot inline
Put the %matplotlib inline on the first line. This is useful in the context of using Jupyter notebooks (http://jupyter.readthedocs.io/en/latest/)

Related

Why Plotly is not working on Jupyter Lab?

I had to update my Macbook Air's iOS (Catalina), since I updated to this iOS's version, plotly stop working on my Jupyter-lab's enviroment.
I already followed the Getting Started's steps showed on plotly website:
$ pip install jupyterlab==1.2 "ipywidgets>=7.5"
When I run a random example, it shows an empty white block:
The libraries that I was importing (The ones that worked before):
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import cufflinks as cf
from plotly.offline import download_plotlyjs,init_notebook_mode, plot,iplot
import plotly.graph_objs as go
init_notebook_mode(connected=True)
cf.go_offline()
%matplotlib inline
You need to have the relevant JupyterLab extensions installed, as per our Getting Started guide here: https://plot.ly/python/getting-started/
If you're having trouble, here is our troubleshooting guide: https://plot.ly/python/troubleshooting/

Jupyter Notebook - Matplotlib keep running

I just started to use Jupiter Notebook to learn Python. while I am trying out matplotlib with this basic code:
import numpy as np
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
The kernel just keep running and nothing happen. How to resolve this? Is there an dependency issue? My newly installed matplotlib is 1.5.1, python is 3.5.2, numpy is 1.11. Running on MacBook Pro 10.11(El Capitan).
To Visualize the plots created by the matplotlib in Jupiter Notebook or ipython notebook you have add one extra line at the beginning.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
If your matplotlib version is above 1.4, and you are using IPython 3.x you have to use the below code.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
It likely showed you a Matplotlib popup, with the IP(y) logo. To show it inline you have to use some IPython magic. Run this in a cell somewhere:
%matplotlib inline
After you close the popup it will finish the statement in your kernel
It sometimes takes time until the kernel starts.
Check that the code is color-highlighted. If it is, it means that the kernel is running. Evaluate the cell again. You will notice a * beside that cell, meaning it's running.
And one more thing: Sometimes the plot is displayed but its window hides behind the notebook... Access it from the task bar

%matplotlib linline produces error

I have the following code which is regular for anyone who would like to make a plot with pandas in IPython.
In [1]: import pandas as pd
In [2]: import numpy as np
In [3]: import matplotlib.pyplot as plt
In [4]: %matplotlib inline
Here it pops up an error:
UsageError: Invalid GUI request u'inline', valid ones are: pyglet, osx, qt5, qt, glut, gtk, gtk3, tk, wx
I read this post Run python script in IPython with inline / embedded plots
and found the two methods to get around:
ipython qtconsole --matplotlib inline -m example_plots
or
ipython qtconsole --pylab inline -m example_plots
But none of them works in my situation (I actually got stuck in iPython without making a plot, needless to say a plot embedded within a script.)
I am not sure where went wrong as %matplotlib inline used to work. I am using python 2.7.11 and IPython 4.2.0 if this information helps.
Thank you very much for your help.

pylab cannot find reference for its modules

I have a mac OS X Yosimite and I'm using python 2.7.10 and Pycharm as my IDLE. I have pylab installed properly but I cannot use any of its modules.
When a try:
from pylab import show
(or any module) it says
ImportError: cannot import name show
But when I run just the line import pylab I get no errors!
I tried leaving that way and calling the module anyway.
pylab.imshow(...)
But I got the same error obviously. Do I have to install those modules separately?
PS: I'm almost sure the problem has nothing to do with the interpreter
Try importing from matplotlib.pyplot, rather than from pylab (this is now the recommended way to import matplotlib):
From example:
from matplotlib.pyplot import imshow
imshow()
Or:
import matplotlib.pyplot as plt
plt.imshow()

plot() doesn't work on IPython notebook

I'm new to python scientific computing, and I tried to make a simple graph on IPython notebook.
import pandas
plot(arange(10))
Then error had shown as below.
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-6b139d572bd6> in <module>()
1 import pandas
----> 2 plot(arange(10))
NameError: name 'plot' is not defined
Instead, with IPython --pylab mode, a right graph popped up when I tried the same code.
Am I missing any environment?
My environment is Mac OSX 10.8.5, python 2.7.5, IPython 1.1.0, matplotlib 1.3.1, and pandas 0.12.0. I downloaded python scientific environment by Anaconda installer from continuum.io. Anaconda version is the newest one as of 1/30/2014.
It is not advisable to use pylab mode. See the following post from Matthias Bussonnier
A summary from that post:
Why not to use pylab flag:
It is irreversible- Cannot unimport
Unclear- if someone else did not run with this flag (or with a different setting of it) what would happen?
Pollutes the namespace
Replaces built-ins
Side effects
You are much better by doing the following inside your IPython notebook.
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(range(10))
The following is the code which --pylab brings into the namespace
import numpy
import matplotlib
from matplotlib import pylab, mlab, pyplot
np = numpy
plt = pyplot
from IPython.core.pylabtools import figsize, getfigs
from pylab import *
from numpy import *
Still, if you wish to use pylab and have plots inline, you may do either of the following:
From shell:
$ ipython notebook --pylab inline
Or, from within your notebook
%pylab inline

Categories