The AttributeError: module ‘matplotlib’ has no attribute ‘plot’ mainly occurs if you have not imported the matplotlib
properly in your code, or if you have not correctly installed the matplotlib
and try to import that into your code.
How to Reproduce the Error
Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. Matplotlib makes easy things easy and hard things possible.
Let us try to reproduce the error with a simple example. In the below example, we will attempt to create a line plot using the matplotlib
.
import matplotlib as plt
# Data to plot on x and y axis
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [5, 1, 2, 3, 1, 6, 7, 8, 6]
# create line plot
plt.plot(x, y)
# show line plot
plt.show()
Output
AttributeError: module 'matplotlib' has no attribute 'plot'
When we run the above code, we will get AttributeError. This is because we have wrongly imported the matplotlib
in the code.
How to Fix the Error
We get the error typically when we import the matplotlib
library like this
import matplotlib as plt
Instead, we should be importing as
import matplotlib.pyplot as plt
We can also fix our earlier code by simply changing our import statement, as shown below.
import matplotlib.pyplot as plt
# Data to plot on x and y axis
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [5, 1, 2, 3, 1, 6, 7, 8, 6]
# create line plot
plt.plot(x, y)
# show line plot
plt.show()
Output

Notice that the error is now gone, and we are able to create the line plot successfully without any errors.
If you still face the error, verify if the matplotlib
module is installed correctly in your environment where the code is getting executed.
Conclusion
The AttributeError: module ‘matplotlib’ has no attribute ‘plot’ mainly occurs if you have not imported the matplotlib
in a correct way inside your code or if you have not correctly installed the matplotlib
and try to import that into your code.
We can resolve the error by changing our import statement from import matplotlib as plt
to import matplotlib.pyplot as plt
and also we need to ensure that the matplotlib module is installed correctly in the environment where the code is getting executed.