Fix: module ‘matplotlib’ has no attribute ‘plot’?

This error occurs when the code is trying to use a function or attribute that does not exist in the matplotlib module. To fix this, make sure that the correct module is imported, that the function or attribute being used is available in the current version of matplotlib, and that the correct spelling is used.


One error you may encounter when using 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.

x