Skip to content

Introduction to Matplotlib

What is Matplotlib?

Matplotlib is the core plotting library in Python.

  • Flexible and powerful
  • Works everywhere (scripts, notebooks)
  • Backbone for many libraries (including Seaborn)

Matplotlib was started by John Hunter in 2002 to bring a MATLAB-like plotting interface to Python. It can export charts to almost every common format — PNG, SVG, PDF, and more — which is why it’s still the default choice for publication-quality figures, even with newer libraries around.

How a plot gets built

diagram From data to a rendered chart mermaid
Matplotlib always builds a chart the same way: a Figure holds Axes, and you draw Artists onto the Axes.

Two common ways to plot

1) Pyplot (quick)

pyplot quick
import matplotlib.pyplot as plt
 
plt.plot([1, 2, 3], [3, 2, 5])
plt.title("Quick plot")
plt.show()
pyplot quick
import matplotlib.pyplot as plt
 
plt.plot([1, 2, 3], [3, 2, 5])
plt.title("Quick plot")
plt.show()
OO API
import matplotlib.pyplot as plt
 
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot([1, 2, 3], [3, 2, 5])
ax.set_title("OO plot")
ax.set_xlabel("x")
ax.set_ylabel("y")
plt.show()
OO API
import matplotlib.pyplot as plt
 
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot([1, 2, 3], [3, 2, 5])
ax.set_title("OO plot")
ax.set_xlabel("x")
ax.set_ylabel("y")
plt.show()

Key terms

  • Figure: the whole canvas
  • Axes: the plotting area
  • Artist: everything drawn on the figure

In this phase, we’ll focus on practical charts you need for analytics.

Next

Continue to Anatomy of a Plot to see exactly how Figure, Axes, ticks, and labels fit together.

🧪 Try It Yourself

Exercise 1 – Object-oriented basics

Exercise 2 – Labelling an Axes

Exercise 3 – A grid of Axes

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did