I have no programming experience and am learning Python for a class. I'm going through conditionals now and have a problem that I'm not understanding the cause of.
I'm basically just trying to let someone plot x^2 within a desired interval of x with the restriction that x isn't negative. If the user inputs a negative input, they'll get a message saying it's not allowed. Otherwise, x vs. x^2 should be plotted.
N = input('Enter upper limit of x:');
if N < 0:
print "Negative input isn't allowed."
else:
x = arange(N)
y = x*x
plot(x,y)
The above gives me the error:
NameError: name 'arange' is not defined
Which I don't understand because arange()
is normally defined from my (very little) understanding.
Python has a lot of built in commands. However, arange
and plot
are not some of them. These are provided by numpy
and matplotlib
.
To get your code to work, you could add:
from numpy import *
from matplotlib import *
at the top of your code. This isn't the recommended way though. It's better to do:
import numpy
import matplotlib
and then when you call their functions
x=numpy.arange(N)
and
matplotlib.plot(x,y)
The reason that this is recommended is sometimes you may have other modules that have the same function names.
A (very) slightly more advanced version that is even better is like:
import numpy as np
import matplotlib.pyplot as plt
x=np.arange(N)
plt.plot(x,y)
np
is a standard abbreviation for numpy
and plt
is a standard abbreviation of matplotlib.pyplot
(which is where the command plot
is actually defined). These are the standard abbreviations, and importing just part of matplotlib
is good for reducing overhead.
You should read up on what a numpy array is versus a usual python list.
Have you been exposed to python
through the ipython
interactive shell? with ipython
it is still possible to use the deprecated command line switch -pylab
that exposes to the user the matplotlib
interactive commands from the matplotlib.pyplot
module and all the numpy
library.
When you bring these habits in writing your first python program you will find that things are more complicated...
You have essentially two solutions, the first one is
from pylab import *
...
that allows you to call unqualified function names like arange
and plot
from your script and, second one
import numpy as np
import matplotlib.pyplot as plt
# ...
x = np.arange(N)
# ...
plt.plot(x, x*x)
even if the second possibility is more verbose than the first, it is the recommended avenue: unqualified imports are considered bad practice because they pollute the namespace of your script, and this is particularly true with large modules like matplotlib.pyplot
and numpy
that defines hundreds of names!
Re the names used for the imports, that is np
and plt
, these choices are a sort of best practice agreement that you'd be wise to adopt, as you'll find on the net and on SO 1000's of examples that use exactly these names to access the plotting and numerical libraries.
To summarize, using
import numpy as np
import matplotlib.pyplot as plt
is a so common idiom that it is what I advice you to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With