How to Fix: module ‘matplotlib’ has no attribute ‘plot’


One error you may encounter when using matplotlib is:

AttributeError: module 'matplotlib' has no attribute 'plot'

This error typically occurs when you use the following code to import matplotlib:

import matplotlib as plt

Instead, you should use:

import matplotlib.pyplot as plt

The following example shows how to fix this error in practice.

How to Reproduce the Error

Suppose we attempt to create a line plot in matplotlib using the following code:

import matplotlib as plt

#define data
x = [1, 2, 3, 4, 5, 6]
y = [3, 7, 14, 19, 15, 11]

#create line plot
plt.plot(x, y)

#show line plot
plt.show()

AttributeError: module 'matplotlib' has no attribute 'plot' 

We receive an error because we used the wrong line of code to import the matplotlib library.

How to Fix the Error

To fix this error, we simply need to use the correct code to import the matplotlib library:

import matplotlib.pyplot as plt

#define data
x = [1, 2, 3, 4, 5, 6]
y = [3, 7, 14, 19, 15, 11]

#create line plot
plt.plot(x, y)

#show line plot
plt.show()

Notice that we’re able to create the line plot successfully without receiving any errors because we used the correct line of code to import the matplotlib library.

Additional Resources

The following tutorials explain how to fix other common errors in Python:

How to Fix: No module named matplotlib
How to Fix: No module named pandas
How to Fix: No module named numpy

Leave a Reply

Your email address will not be published.