Matplotlib is an open-source python package for data visualization. You can plot bar charts, histograms, scatter plots,line chart, e.t.c using it. But sometimes you can get AttributeError on part of the module. In this example you will learn how to solve the AttributeError: module ‘matplotlib’ has no attribute ‘plot’ error for various cases.
What is an AttributeError?
AttributeError is a part of the exception handling in Python. Mose of the case you will get this error when you are passing the wrong type of the variable. For example, I have a function append(). If I will pass the string variable as an argument then you will get this error. It’s because you are giving a string-type argument instead of list type argument.
Causes of Module ‘matplotlib’ has no attribute ‘plot’
There can be two possibilities for getting this AttributeError: module ‘matplotlib’ has no attribute ‘plot’. You will know each case.
Case 1: Not installed or Corrupt Matplotlib
The first case that gives you this error is that you have not installed the matplotlib python package in your system. And when you try to use the plot function then you will get the error.
To check whether matplotlib library is installed or not run the below lines of code.
import matplotlib print(matplotlib.__version__)
You will get the version of the matplotlib package installed in your system, otherwise, you will get the ImportError: No module named matplotlib error.
The solution for the above is very simple. You have to install the Matplotlib python package in your system. Use the below pip command to install it.
For python 3. xx version
pip3 install matplotlib
For python 2. xx version
pip install matplotlib
Now you will not get the module ‘matplotlib’ has no attribute ‘plot’ error.
Case 2: Not properly importing matplotlib
The second case when you will get the module ‘matplotlib’ has no attribute ‘plot’ error is when you are not importing matplotlib package while coding. Most of the case coders use the following way for importing matplotlib that gives the error.
import matplotlib as plt import numpy as np x = np.linspace(-20 , 20, 200) y = np.sin(x) plt.plot(x, y, marker="o") plt.show()
You can see in the above code matplotlib package is not properly imported.
The solution to the above case is that you have to properly import matplotlib package. To use the plot function you have to write import matplotlib.pyplot instead of only import matplotlib.
Now when you will run the below lines of code then you will not get the module ‘matplotlib’ has no attribute ‘plot’ error. You will get the sin figure as an output.
import matplotlib.pyplot as plt import numpy as np x = np.linspace(-20 , 20, 200) y = np.sin(x) plt.plot(x, y, marker="o") plt.show()
There are two cases when you can get the module ‘matplotlib’ has no attribute ‘plot’ error. The one is the not installed matplotlib library in your system and the other is not properly importing the matplotlib package in your system. The most obvious case will be the second one. Just follow the above solution to solve this attributeerror.
I hope you have liked this tutorial. If you have any questions then you can contact us for more help.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.