-2

When I imported matplotlib by using only import matplotlib, whether the pyplot was imported or not.

Following is a code to show my problem :

import numpy as np
import matplotlib as plt
print(np.sin(180))
x_sin=np.arange(0, 3*3.14 ,0.1)
print(x_sin)
y_sin=np.sin(x_sin)
print(y_sin)
plt.pyplot.plot(x_sin,y_sin)

When I tried to run this code it gave me the error:

AttributeError: module 'matplotlib' has no attribute 'pyplot'

2
  • matplotlib and matplotlib.pyplot are different modules, simple as that. Note that matplotlib.pyplot is a single dotted name, and not an attribute lookup on the matplotlib module. Commented Apr 18, 2023 at 1:14
  • @Brian61354270: Well, in the context of an import statement, matplotlib.pyplot isn't an attribute lookup. If you try to use matplotlib.pyplot later in an expression, that'll be an attribute lookup. Commented Apr 18, 2023 at 1:26

2 Answers 2

2

You need to be explicit of the module you want to import, in your case is the pyplot.

The usual way using of importing pyplot:

import matplotlib.pyplot as plt  

or

from matplotlib import pyplot as plt
Sign up to request clarification or add additional context in comments.

Comments

0

Try this way

import numpy as np
import matplotlib.pyplot as plt
print(np.sin(180))
x_sin = np.arange(0, 3*3.14, 0.1)
print(x_sin)
y_sin = np.sin(x_sin)
print(y_sin)
plt.plot(x_sin, y_sin)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.