I am new to Python and I am learning matplotlib. I am following the video tutorial recommended in the official User Manual of matplotlib: 'Plotting with matplotlib' by Mike Muller. The instructor does not show how he imports matplotlib but proceeds instantly with commands such as plot(x, linear, x, square), where x a sequence he has defined.
I tried to run
import matplotlib
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
x = np.arange(100)
lines = plot(x, 'linear', 'g:+', x, 'square','r-o')
and got the error
NameError: name 'plot' is not defined
You need to prefix all plotting calls with plt.
like
lines = plt.plot(x, 'linear', 'g:+', x, 'square','r-o')
In Python functions that are not "builtin", i.e. always present, must be imported from modules. In this case the line
from matplotlib import pyplot as plt
is the same as
import matplotlib.pyplot as plt
and means that you are importing the pyplot
module of matplotlib
into your namespace under the shorter name plt
.
The pyplot
module is where the plot(), scatter()
, and other commands live.
If you don't want to write plt.
before every plot call you could instead do
from matplotlib.pyplot import *
which will import all functions (symbols) into the global namespace, and you can now use your original line:
lines = plot(x, 'linear', 'g:+', x, 'square','r-o')