Lesson 01
What Is Matplotlib
Meet Matplotlib, the plotting library for Python, and draw a first chart from a list of numbers with pyplot in three short lines.
Matplotlib is the plotting library for Python. It takes plain lists, NumPy arrays, or pandas columns and turns them into charts you can read, save, and publish. Almost everyone reaches it through one module, pyplot, imported as plt, and almost every chart follows the same short rhythm: pick a chart type, hand it your data, label it, show it.
Install and import
On your computer, install it once with pip install matplotlib. In a program, import the pyplot module and give it the short name plt. In the playground on this page the library is already available, though the first run that imports it takes a few seconds.
import matplotlib.pyplot as plt
print(plt.__name__)Output:
matplotlib.pyplotA chart in three lines
Give plot a list of x values and a list of y values of the same length, then call show. Matplotlib fills in the axis range, the tick numbers, and the line colour for you, so the shortest useful chart really is three lines long.
import matplotlib.pyplot as plt
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
signups = [42, 58, 51, 66, 73, 61, 47]
plt.plot(days, signups)
plt.show()Run that in the playground below and the chart appears under the output. The table of seven numbers took a moment to read; the line shows the Friday peak immediately.
The chart types you will meet
Matplotlib draws a long list of chart types, but five cover most everyday work, and this module builds each one in turn. Each has a job, and picking the wrong one hides the very thing you wanted to show.
import matplotlib.pyplot as plt
jobs = {
"plot": "a value changing across an ordered axis",
"bar": "comparing separate categories",
"barh": "the same comparison with long category names",
"scatter": "how two measurements move together",
"hist": "how one measurement is spread out",
"pie": "parts of a single whole",
}
for name, job in jobs.items():
print(f"{name:<8} {job}")
print(len(jobs), "chart types ahead")Output:
plot a value changing across an ordered axis
bar comparing separate categories
barh the same comparison with long category names
scatter how two measurements move together
hist how one measurement is spread out
pie parts of a single whole
6 chart types aheadData from anywhere
Matplotlib does not care where the numbers came from. Lists, NumPy arrays, and pandas columns all work the same way, which is why it sits at the end of so many data pipelines.
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
as_list = [3, 7, 4, 9]
as_array = np.array([3, 7, 4, 9])
as_column = pd.Series([3, 7, 4, 9])
for data in (as_list, as_array, as_column):
plt.plot(data)
print(type(data).__name__, "plotted")
plt.close("all")Output:
list plotted
ndarray plotted
Series plottedplt.close("all") throws away figures you do not want to keep. It matters in loops, where an unclosed figure quietly stays in memory.
Key takeaways
-
Matplotlib turns lists, arrays, and DataFrame columns into charts
-
Import it as
import matplotlib.pyplot as plt -
The rhythm is: choose a chart, pass data, label it, show it
-
Five chart types cover most everyday work
-
Next: Anatomy of a Figure
-
Previous: Exploring a Dataset
Loading playground…
Check your understanding
What Is Matplotlib Quiz
Which module do you normally import from Matplotlib?
1 / 10