Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Intermediate25 min
What Is Matplotlib hero Module 25 Lesson 1What Is Matplotlib hero Module 25 Lesson 1

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.

The five steps from installing Matplotlib to showing a chartThe five steps from installing Matplotlib to showing a chart

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.

python
import matplotlib.pyplot as plt

print(plt.__name__)

Output:

text
matplotlib.pyplot

A 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.

python
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()
A line chart of signups rising to a Friday peakA line chart of signups rising to a Friday peak

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.

python
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:

text
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 ahead

Data 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.

python
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:

text
list plotted
ndarray plotted
Series plotted

plt.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