-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'

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
  • `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. – Brian61354270 Apr 18 '23 at 01: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. – user2357112 Apr 18 '23 at 01:26

2 Answers2

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
Bryce Chan
  • 1,639
  • 11
  • 26
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)
Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24