TomAnth
TomAnth

Reputation: 11

NameError: name 'plt' is not defined

I'm using Python 3.8.1 on MacOS Catalina.

I installed matplotlib through Terminal, and then in the Python shell I imported it with the following code:

>>> import matplotlib

>>> import matplotlib.pyplot as plt

I initially just did the second line, but thought I'd try the first one too. Either way, no error messages so assumed that I can now use pyplot.

Then I entered this code and ran the module:

X = [[6], [8], [10], [14], [18]]
y = [[7], [9], [13], [17.5], [18]]
plt.figure()
plt.title('Pizza price plotted against diameter')
plt.xlabel('Diameter in inches')
plt.ylabel('Price in dollars')
plt.plot(X, y, 'k.')
plt.axis([0, 25, 0, 25])
plt.grid(True)
plt.show()

But I get this error message:

Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    plt.figure()
NameError: name 'plt' is not defined

Does anybody know why?

Upvotes: 0

Views: 21346

Answers (1)

Sanjit Sarda
Sanjit Sarda

Reputation: 391

I ran this code and it worked:

import matplotlib.pyplot as plt

X = [[6], [8], [10], [14], [18]]
y = [[7], [9], [13], [17.5], [18]]
plt.figure()
plt.title('Pizza price plotted against diameter')
plt.xlabel('Diameter in inches')
plt.ylabel('Price in dollars')
plt.plot(X, y, 'k.')
plt.axis([0, 25, 0, 25])
plt.grid(True)
plt.show()

Try this and tell me if it still doent work.

Upvotes: 3

Related Questions