plot() doesn't work on IPython notebook - python

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

Related

Matplotlib not displaying image

I am trying to use matplotlib to view an image in ipython. However, I solely obtain a description of the image as opposed to seeing the actual image:
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> plt.imshow(an_array)
Out[4]: <matplotlib.image.AxesImage at 0x7fb626f063d0>
This is with the backend set to Qt5Agg or TkAgg in .matplotlibrc
I have the same issues whether or not I am working in a conda virtual environment
I am running: Scientific Linux release 6.8 (Carbon), Python 2.7.13, Anaconda 4.3.0 (64-bit)
I have had similar issues on my local computer running MacOSX
I would be most grateful for any advice,
Thanks
Rob
Add %matplotlib inline before import matplotlib.pyplot as plt to enable automatic visualization of your plots when the cell has finished executing.
Note, however, that this works just in iPython notebooks. Normally, you'd need to call plt.show() (without any arguments) to visualize the plots.

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.

%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
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.
Because line magics are only supported by the IPython command line not by Python cl, use: 'exec(%matplotlib inline)' instead of %matplotlib inline
The syntax '%' in %matplotlib inline is recognized by iPython (where it is set up to handle the magic methods), but not Python itself, which gives a SyntaxError.
Here is given one solution.
If you include the following code at the top of your script, matplotlib will run inline when in an IPython environment (like jupyter, hydrogen atom plugin...), and it will still work if you launch the script directly via command line (matplotlib won't run inline, and the charts will open in a pop-ups as usual).
from IPython import get_ipython
ipy = get_ipython()
if ipy is not None:
ipy.run_line_magic('matplotlib', 'inline')
There are several reasons as to why this wouldn't work.
It is possible that matplotlib is not properly installed.
have you tried running:
conda install matplotlib
If that doesn't work, look at your %PATH% environment variable, does it contain your libraries and python paths?
Similar problem on github anaconda
This is the case you are using Julia:
The analogue of IPython's %matplotlib in Julia is to use the PyPlot package, which gives a Julia interface to Matplotlib including inline plots in IJulia notebooks. (The equivalent of numpy is already loaded by default in Julia.)
Given PyPlot, the analogue of %matplotlib inline is using PyPlot, since PyPlot defaults to inline plots in IJulia.
Instead of %matplotlib inline,it is not a python script so we can write like this it will work
from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')

Jupyter Notebook ImportError, %matplotlib qt

I am using
%matplotlib inline
to display plots inside the notebook. I would like to disable this for several cells. So, I try
%matplotlib qt
This outputs the following error:
ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5,
or PySide package to be installed, but it was not found.
I am not sure how to solve this, as everything is up to date.
How can I solve the above?
Is there another way to disable %matplotlib inline in a certain cell without restarting the entire kernel?
You might be able to use plt.switch_backend, although as the documentation states, this is an experimental feature. The following works for me, using matplotlib 1.5 and an IPython 4.0.1:
In [1]: from matplotlib import pyplot as plt
In [2]: import numpy as np
# plot appears inline (default)
In [3]:plt.plot(np.random.randn(10))
Out[3]:[<matplotlib.lines.Line2D at 0x7fac4408e390>]
In [4]: plt.switch_backend('QtAgg4')
# plot appears inside a separate Qt4 window
In [5]:plt.plot(np.random.randn(10))
Out[5]:[<matplotlib.lines.Line2D at 0x7fac3b408a90>]
You might need to change 'QtAgg4' according to whichever version of PyQt you have installed - this could be the cause of the error you mentioned in the question. Another interactive backend that should work on Mac would be 'CocoaAgg'. If the images are very large you could also use the 'Agg' backend to suppress plotting altogether, and instead save the resulting figure(s) straight to disk.
If you don't have a specific backend installed use "agg":
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
Reference: https://github.com/matplotlib/matplotlib/issues/9017

Categories