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.

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.
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.pycor__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 matplotlibAfter 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 ofplt.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.

If it’s outdated, upgrade to the latest stable release:
pip install --upgrade matplotlibRead 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.pyplotasplt. - 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:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.