How to Fix the “module ‘matplotlib’ has no attribute ‘plot’” Error in Python

If you’ve ever worked with Python’s matplotlib library for data visualization, you might have encountered the frustrating error message: “module ‘matplotlib’ has no attribute ‘plot’.” I’ve been there myself, and I know how confusing it can be, especially when you just want to create a simple line chart to visualize, say, monthly sales data for your business.

Let me walk you through the best ways to resolve this error, so you can get back to creating beautiful plots without headaches.

Let’s get in!

Fix the “module ‘matplotlib’ has no attribute ‘plot’” Error in Python

The error “module ‘matplotlib’ has no attribute ‘plot’” typically means that Python couldn’t find the plot function where you expected it. This usually happens because of how matplotlib is imported or due to naming conflicts.

Matplotlib’s plotting functions like plot() are part of the matplotlib.pyplot module, not the top-level matplotlib module. So, if you try to call matplotlib.plot(), Python will throw this error.

Check out Matplotlib Two y axes

Method 1: Correctly import matplotlib.pyplot

The most common fix is to import the pyplot submodule explicitly and use it to call plot().

import matplotlib.pyplot as plt
import numpy as np

# Example: Plotting monthly sales data for a US retail store
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
sales = [25000, 27000, 30000, 28000, 32000, 35000]

plt.plot(months, sales, marker='o')
plt.title('Monthly Sales Data - US Retail Store')
plt.xlabel('Month')
plt.ylabel('Sales ($)')
plt.show()

You can refer to the screenshot below to see the output.

matplotlib has no attribute get_data_path

In this example, plt.plot() works because we imported pyplot as plt. If you try matplotlib.plot(), it won’t work because plot is not a direct attribute of the matplotlib module.

Read Matplotlib Invert y axis

Method 2: Avoid Naming Conflicts with Your Script

Another common cause is that your Python script or a local file is named matplotlib.py. This causes Python to import your file instead of the actual matplotlib library.

To fix this:

  • Rename your script to something else, like plot_sales.py.
  • Delete any matplotlib.pyc or __pycache__ folders in your project directory.
  • Restart your Python environment.

This will prevent Python from confusing your script with the real matplotlib module.

Method 3: Verify matplotlib Installation

Sometimes, the error occurs because matplotlib is not installed properly or is corrupted.

Run this command in your terminal or command prompt to reinstall matplotlib:

pip install --upgrade --force-reinstall matplotlib

After reinstalling, try your plot code again. This ensures you have a clean, working version of matplotlib.

Read Put the Legend Outside the Plot in Matplotlib

Method 4: Use an IDE or an Environment That Supports matplotlib

If you’re running code in an environment that doesn’t support GUI windows (like some remote servers or minimal Docker containers), plt.show() might fail silently or cause errors.

For US-based data analysts working remotely or on cloud platforms, consider:

  • Using Jupyter Notebooks, where plots render inline.
  • Saving plots as image files using plt.savefig('plot.png') instead of plt.show().

Example:

plt.plot(months, sales)
plt.savefig('monthly_sales.png')

Check out Matplotlib Save as PDF

Method 5: Check Your matplotlib Version

Older versions of matplotlib might behave differently or have bugs.

Check your version with:

import matplotlib
print(matplotlib.__version__)

You can refer to the screenshot below to see the output.

attributeerror module 'matplotlib' has no attribute 'plot'

If it’s outdated, upgrade to the latest stable release:

pip install --upgrade matplotlib

Read Matplotlib Title Font Size

Summary

The “module ‘matplotlib’ has no attribute ‘plot’” error is a common stumbling block, but one that’s easy to fix with the right approach. Always remember to:

  • Import matplotlib.pyplot as plt.
  • Avoid naming your scripts matplotlib.py.
  • Reinstall matplotlib if needed.
  • Use appropriate environments for plotting.
  • Keep your matplotlib version up to date.

Following these tips will help you avoid this error and make your data visualization tasks smooth and productive.

You may like to read other Matplotlib articles:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.