[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

Get notified of the best deals on our WordPress themes.

You May Also Like
Python Read Text File

Python Read Text file

Table of Contents Hide Steps to Read Text File in Python Python open() function Methods for Reading file contentsPython close() functionExamples for Reading a Text file in Python Example 1 – Read…
View Post
Python Remove Newline From String

Python Remove Newline From String

Table of Contents Hide Python Remove Newline From StringUsing strip() method to remove the newline character from a stringUsing replace() method to remove newlines from a stringUsing regex to remove…
View Post