[Solved] AttributeError: module ‘matplotlib’ has no attribute ‘plot’

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.

In this tutorial, let’s look at what is AttributeError: module ‘matplotlib’ has no attribute’ plot’ and how to resolve that in our code with examples.

What is AttributeError: module ‘matplotlib’ has no attribute ‘plot’?

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 module ‘matplotlib’ has no attribute ‘plot’ error. The AttributeError is because we have wrongly imported the matplotlib in your code.

Fix – AttributeError: module ‘matplotlib’ has no attribute’ plot’

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

Module 'Matplotlib' Has No Attribute 'Plot'
[Solved] AttributeError: module 'matplotlib' has no attribute 'plot' 2

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.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Numpy Mean

NumPy mean()

Table of Contents Hide NumPy mean() SyntaxNumPy mean() ParametersNumPy mean() Return ValueExample 1: Using the numpy.mean() method on a flattened arrayExample 2: Compute the mean of the array column-wise using…
View Post