How to Install Matplotlib in Python
What you’ll build or solve
You’ll install Matplotlib with pip, verify the install in one command, and confirm plotting works by running a tiny script.
When this approach works best
This approach works best when you need to:
Learn Python on Mimo
- Make quick charts for learning, homework, or a small script.
- Add plotting to a Python script that already works in your environment.
- Confirm Matplotlib can open or save plots on your system.
Avoid this approach when:
- You need a locked-down, system-wide install. A virtual environment often works better.
- You are using an environment manager like Conda and your team expects Conda installs.
Prerequisites
- Python 3 installed
- You can run terminal commands
- You can run a Python file with
python your_file.py
Step-by-step instructions
1) Install Matplotlib with pip
Run:
python-m pip install matplotlib
python-c"import matplotlib; print(matplotlib.__version__)"
If your system uses python3:
python3-m pip install matplotlib
python3-c"import matplotlib; print(matplotlib.__version__)"
If a version number prints without errors, the install worked.
For detailed pip usage and general install troubleshooting, see the guide on installing Python modules.
2) Test with a simple plot
Create a file named plot_test.py:
importmatplotlib.pyplotasplt
x= [1,2,3,4]
y= [1,4,9,16]
plt.plot(x,y)
plt.title("Test Plot")
plt.show()
Run it:
python plot_test.py
What to look for:
A window opens with a simple line plot. In a notebook environment, the plot may appear inline.
3) Common Matplotlib-specific patterns
Option A: Import pyplot the right way
importmatplotlib.pyplotasplt
If you wrote import pyplot, change it to the line above.
Option B: Always call plt.show() in scripts
In a normal .py file, the window does not appear unless you call:
plt.show()
Option C: Save a plot when a window cannot open
If you are on a server or your environment has no GUI, save the plot instead:
CSS
importmatplotlib.pyplotasplt
plt.plot([1,2,3], [1,4,9])
plt.savefig("plot.png")
What to look for:
A new file named plot.png appears in your current folder.
Examples you can copy
1) Install and verify in one go
python-m pip install matplotlib
python-c"import matplotlib; print(matplotlib.__version__)"
2) Minimal plot (fastest visual check)
CSS
importmatplotlib.pyplotasplt
plt.plot([1,2,3], [1,4,9])
plt.show()
3) Bar chart test
importmatplotlib.pyplotasplt
labels= ["A","B","C"]
values= [3,7,5]
plt.bar(labels,values)
plt.title("Bar Chart Test")
plt.show()
Common mistakes and how to fix them
Mistake 1: Installing Matplotlib but getting ModuleNotFoundError
You might run:
pip install matplotlib
python-c"import matplotlib"
Why it breaks:
pip may point to a different Python than the one you run.
Correct approach:
python-m pip install matplotlib
python-c"import matplotlib; print(matplotlib.__version__)"
Mistake 2: Importing the wrong module name
You might write:
importpyplot
Why it breaks:
pyplot is inside Matplotlib, so importing it directly fails.
Correct approach:
importmatplotlib.pyplotasplt
Mistake 3: No plot window shows up
You might write:
CSS
importmatplotlib.pyplotasplt
plt.plot([1,2,3], [1,4,9])
Why it breaks:
Scripts need plt.show() to display the window.
Correct approach:
CSS
importmatplotlib.pyplotasplt
plt.plot([1,2,3], [1,4,9])
plt.show()
Troubleshooting
If you see ModuleNotFoundError: No module named 'matplotlib', run the install and verify commands again using the same python executable.
If you see No module named pip, run:
CSS
python-m ensurepip--upgrade
python-m pip install--upgrade pip
If a plot window does not appear, confirm your script calls plt.show(). If your environment has no GUI, use plt.savefig("plot.png") instead.
If you are on Linux and Matplotlib mentions a missing backend or display, try saving the plot to a file. For interactive windows, install a GUI toolkit appropriate for your distribution.
Quick recap
Install and verify:
python-m pip install matplotlib
python-c"import matplotlib; print(matplotlib.__version__)"
Test plotting with a tiny script using:
CSS
importmatplotlib.pyplotasplt
plt.plot([1,2,3], [1,4,9])
plt.show()
If a window cannot open, save plots with plt.savefig(...) instead.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot