CHAPTER 04
Beginner
Introduction to Matplotlib
Updated: May 18, 2026
5 min read
# CHAPTER 4
Introduction to Matplotlib
1. Chapter Introduction
Matplotlib is Python's foundational visualization library — every other library (Seaborn, Pandas plotting) is built on it. Understanding Matplotlib's Figure/Axes architecture gives you complete control over every aspect of your charts.2. Matplotlib Architecture
text
3. First Chart — Two APIs
python
4. Key Matplotlib Functions
python
5. Mini Project: Sales Trend Visualization
python
6. Common Mistakes
-
Forgetting
tightlayout(): Without it, titles and labels overlap. Always callplt.tightlayout()beforesavefig().
-
plt.show()beforesavefig():show()clears the figure. Always save first, then show.
7. MCQs
Question 1
fig, ax = plt.subplots() creates?
Question 2
ax.spines['top'].setvisible(False) does?
Question 3
plt.tightlayout() fixes?
Question 4
plt.FuncFormatter(lambda x, : f'${x:,.0f}') formats axis as?
Question 5
ax.annotate() is for?
Question 6
figsize=(10, 6) sets?
Question 7
savefig(dpi=150) controls?
Question 8
fillbetween(x, y1, y2) creates?
Question 9
ax.text(x, y, 'text') places text at?
Question 10
Recommended Matplotlib API for complex charts?
8. Interview Questions
- Q: What is the difference between the pyplot and object-oriented Matplotlib APIs?
- Q: How do you add annotations and reference lines to a Matplotlib chart?
9. Summary
Matplotlib's Figure→Axes hierarchy gives complete chart control. Use OO API (fig, ax) for production code. Key functions:ax.plot(), ax.bar(), ax.scatter(), ax.annotate(), ax.settitle/xlabel/ylabel(). Always: tightlayout() → savefig() → show().