Matplotlib Get Started
Before drawing anything you need Matplotlib installed and imported. In the browser playgrounds of this course everything is ready, but on your own computer it takes one command.
Installing Matplotlib
Matplotlib is published on PyPI. Install it with pip (NumPy is installed automatically as a dependency):
pip install matplotlibIf you use the Anaconda distribution, Matplotlib is already included; otherwise conda install matplotlib works too. In a Jupyter notebook you can run %pip install matplotlib inside a cell.
Tip: Install into a virtual environment (
python -m venv .venv) so each project keeps its own package versions.
Importing it
The package itself is called matplotlib, but the part you use for plotting is the pyplot sub-module. The whole Python community imports it under the short name plt:
import matplotlib
import matplotlib.pyplot as plt
print("Matplotlib version:", matplotlib.__version__)
print("pyplot module:", plt.__name__)Matplotlib version: 3.8.4
pyplot module: matplotlib.pyplot
Checking __version__ is handy when a tutorial uses a feature that only exists in newer releases. This course was written for Matplotlib 3.8.
Your first figure
A figure needs only data and one plotting call. plt.plot(x, y) draws a line through the points (x[0], y[0]), (x[1], y[1]), and so on:
import matplotlib.pyplot as plt
hours = [0, 1, 2, 3, 4]
temperature = [14, 15, 17, 20, 22]
plt.plot(hours, temperature)
plt.show()
print("Drew a line through", len(hours), "points")Drew a line through 5 points
What plt.show() does
| Environment | Effect of plt.show() |
|---|---|
| Python script in a terminal | Opens a window and blocks until you close it |
| Jupyter notebook | Displays the figure under the cell (often optional) |
| This course's playground | Does nothing; open figures are shown automatically at the end |
Keep calling plt.show() at the end of your plotting code. It makes the program behave correctly in every environment.
Checking the version of NumPy too
Matplotlib almost always works together with NumPy, so it is useful to know both versions:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 11) # 0, 1, ..., 10
y = x ** 2
plt.plot(x, y)
plt.show()
print("NumPy", np.__version__)
print("x:", x)
print("y:", y)NumPy 2.2.5
x: [ 0 1 2 3 4 5 6 7 8 9 10]
y: [ 0 1 4 9 16 25 36 49 64 81 100]
The parabola is drawn from 11 points only, so you can see small corners where the straight segments meet. Use more points (for example np.linspace(0, 10, 200)) for a smoother curve.
Common problems
ModuleNotFoundError: No module named 'matplotlib': the package is not installed in the Python interpreter you are running. Check withpython -m pip show matplotlib.- Nothing appears when running a script: you forgot
plt.show(). - Several charts end up on top of each other: every
plt.plot()call draws on the current figure. Start a new one withplt.figure()when you want a separate chart (see the next lesson).
Test yourself
Which command installs Matplotlib with pip?
- pip get matplotlib
- pip install matplotlib
- python install matplotlib
- pip install pyplot
In a normal Python script, what happens when plt.show() runs?
- The figure is saved as a PNG
- Nothing happens
- A window with the figure opens and the script waits until it is closed
- The figure is printed as text
How do you print the installed Matplotlib version?
- print(plt.version)
- print(matplotlib.__version__)
- print(matplotlib.version())
- matplotlib --version inside Python