I'm running Python 2 on Jupyter and I'm attempting to edit my notebook.
I have the following code:
points=150
x=linspace(0,9*pi,points)
y=e**(-x/10)*cos(x)
plot(x,y,linestyle='None',marker=7,alpha=0.5)
show()
But an error appears:
NameError Traceback (most recent call last)
<ipython-input-6-6ad78f0584e6> in <module>()
1 points=150
-> 2 x=linspace(0,9*pi,points)
3 y=e**(-x/10)*cos(x)
4
5 plot(x,y,linestyle='None',marker=7,alpha=0.5)
NameError: name 'linspace' is not defined
I ran the notebook earlier on an institutional network with no error at all.
I'm a bit confused - what is the problem?
The linspace()
function belongs to Numpy. Try importing Numpy first. By convention, it's generally imported as an entire module (np
) and then functions are called within the np
object:
import numpy as np
points = 150
x = np.linspace(0, 9 * np.pi, points)
x
# array([ 0. , 0.18976063, 0.37952126, 0.56928189,
# 0.75904252, 0.94880315, 1.13856378, 1.32832441,
# ...]
Note: Same goes for pi
, should be np.pi
. The code you were using may have just done from numpy import *
, which is why the np.
prefix is missing from all the Numpy-specific functions.