Introduction to Matplotlib
What is Matplotlib?
Section titled “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
Section titled “How a plot gets built”flowchart LR A["Your data"] --> B["Figure (the canvas)"] B --> C["Axes (one plotting area)"] C --> D["Artists (lines, bars, ticks, text)"] E["plt.plot() - quick pyplot API"] -.-> C F["fig, ax = plt.subplots() - OO API"] -.-> C
Two common ways to plot
Section titled “Two common ways to plot”1) Pyplot (quick)
Section titled “1) Pyplot (quick)”import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [3, 2, 5])
plt.title("Quick plot")
plt.show()2) Object-oriented API (recommended)
Section titled “2) Object-oriented API (recommended)”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
Section titled “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.
Continue to Anatomy of a Plot to see exactly how Figure, Axes, ticks, and labels fit together.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Object-oriented basics
Section titled “Exercise 1 – Object-oriented basics”Exercise 2 – Labelling an Axes
Section titled “Exercise 2 – Labelling an Axes”Exercise 3 – A grid of Axes
Section titled “Exercise 3 – A grid of Axes”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading