The AttributeError: module ‘matplotlib’ has no attribute ‘plot’ Error mainly Occurs when if you have not imported matplotlib
correctly in your code. Or If you have not installed the matplotlib
properly. To solve the error, First import the matplotlib way and install properly (pip install matplotlib
) before using it.
In this article, you’ll learn everything about this AttributeError: module ‘matplotlib’ has no attribute ‘plot’ Error in Python. And how to resolve the error all the possible solutions with examples.
Let’s try to solve the error with an example. In the below example, We attempt to create a line plot by using matplotlib.
import matplotlib as plt
# 👇️ Data to plot on x and y axis
x = [1, 2, 3, 4, 5, 6]
y = [2, 5, 9, 19, 15, 11]
# 👇️ create line plot
plt.plot(x, y)
# 👇️ show line plot
plt.show()
Output
AttributeError: module 'matplotlib' has no attribute 'plot'
When we run above the code, we will get this AttributeError: module ‘matplotlib’ has no attribute ‘plot’ Error. Because we have imported the matplotlib wrong way.
How to Fix AttributeError: module ‘matplotlib’ has no attribute ‘plot’ Error?
Solution 1 – Import the matplotlib module in a proper way
This error typically occurs when we import matplotlib like this
import matplotlib as plt
Instead, we should be use
import matplotlib.pyplot as plt
Now We can fix our code by changing 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]
y = [2, 5, 9, 19, 15, 11]
# 👇️ create line plot
plt.plot(x, y)
# 👇️ show line plot
plt.show()
Now your error will be gone without any errors.
Solution 2 – Install the matplotlib module in a proper way
If you still facing this error, Verify matplotlib module is installed properly in your environment.
If you have not installed matplotlib module, First of all install matplotlib module like this.
pip install matplotlib
Now successfully installed the matplotlib module. I hope your error will be solved.
Conclusion
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 properly in the environment.
we hope this article has been informative. Thank you for reading. Kindly comment and let us know if you found it helpful.