Skip to main content
Data Visualization
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
123456789101112131415
Matplotlib Object Hierarchy:

Figure  ──── The entire canvas (window/image file)
  │
  ├── Axes  ── Individual plot area (one or many per Figure)
  │     ├── XAxis / YAxis  ── Tick marks and labels
  │     ├── Title
  │     ├── Lines / Bars / Patches
  │     └── Legend
  │
  └── Canvas (rendering backend)

Two APIs:
  pyplot (state-based, MATLAB-style):  plt.plot(), plt.title()
  OO (object-oriented, recommended):   fig, ax = plt.subplots(); ax.plot()

3. First Chart — Two APIs

python
1234567891011121314151617181920212223242526272829
import matplotlib.pyplot as plt
import numpy as np

x = [1, 2, 3, 4, 5, 6]
y = [10000, 15000, 13000, 18000, 22000, 20000]

# API 1: pyplot (quick and simple)
plt.plot(x, y, color='#2196F3', linewidth=2, marker='o')
plt.title('Monthly Sales')
plt.xlabel('Month')
plt.ylabel('Revenue ($)')
plt.show()

# API 2: Object-Oriented (recommended — more control)
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x, y, color='#2196F3', linewidth=2.5, marker='o',
        markersize=8, markerfacecolor='white', markeredgewidth=2)
ax.set_title('Monthly Sales Revenue', fontsize=14, fontweight='bold', pad=15)
ax.set_xlabel('Month', fontsize=12)
ax.set_ylabel('Revenue ($)', fontsize=12)
ax.set_xticks(x)
ax.set_xticklabels(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'])
ax.yaxis.set_major_formatter(plt.FuncFormatter(lambda x, _: f'${x:,.0f}'))
ax.grid(True, alpha=0.3)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.tight_layout()
plt.savefig('sales_trend.png', dpi=150, bbox_inches='tight')
plt.show()

4. Key Matplotlib Functions

python
1234567891011121314151617181920212223242526272829303132333435363738
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(figsize=(10, 6))

# --- PLOT TYPES ---
x = np.linspace(0, 10, 100)
ax.plot(x, np.sin(x), label='sin(x)')               # Line
ax.scatter([1,3,5,7], [0.8,0.1,-0.9,0.7], s=100, label='points')  # Scatter
ax.bar([1,3,5,7], [0.5,0.3,-0.5,0.4], width=0.4, label='bars')    # Bar

# --- ANNOTATIONS ---
ax.axhline(y=0, color='black', linewidth=0.8)  # Horizontal reference line
ax.axvline(x=np.pi, color='red', linestyle='--', label='π')

# Annotate a specific point
ax.annotate('Peak', xy=(np.pi/2, 1), xytext=(np.pi/2+1, 1.2),
            arrowprops=dict(arrowstyle='->', color='gray'),
            fontsize=11, color='gray')

# Add text directly
ax.text(8, 0.5, 'Decreasing\ntrend', fontsize=10, color='blue',
        ha='center', style='italic')

# --- FORMATTING ---
ax.set_title('Matplotlib Elements Demo', fontsize=14, fontweight='bold')
ax.set_xlabel('X Axis', fontsize=12)
ax.set_ylabel('Y Axis', fontsize=12)
ax.legend(loc='upper right', framealpha=0.8)
ax.set_xlim(0, 10)
ax.set_ylim(-1.5, 1.8)
ax.grid(True, linestyle='--', alpha=0.4)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

plt.tight_layout()
plt.savefig('elements_demo.png', dpi=150)
plt.show()

5. Mini Project: Sales Trend Visualization

python
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
import matplotlib.pyplot as plt
import numpy as np

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
          'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
revenue_2023 = [42000, 45000, 51000, 48000, 53000, 62000,
                58000, 61000, 67000, 72000, 89000, 95000]
revenue_2024 = [47000, 49000, 56000, 54000, 61000, 70000,
                65000, 69000, 75000, 82000, 98000, 110000]

fig, ax = plt.subplots(figsize=(12, 6))

# Plot both years
ax.plot(months, revenue_2023, 'o-', color='#90CAF9', linewidth=2,
        markersize=7, label='2023', alpha=0.8)
ax.plot(months, revenue_2024, 's-', color='#1565C0', linewidth=2.5,
        markersize=7, label='2024')

# Fill between years
ax.fill_between(months, revenue_2023, revenue_2024, alpha=0.1, color='#1565C0')

# Annotate peak
peak_idx = revenue_2024.index(max(revenue_2024))
ax.annotate(f'Peak: ${max(revenue_2024):,}',
            xy=(months[peak_idx], max(revenue_2024)),
            xytext=(months[peak_idx-2], max(revenue_2024) + 5000),
            arrowprops=dict(arrowstyle='->', color='darkblue'),
            fontsize=11, color='darkblue', fontweight='bold')

# Growth annotation
yoy_growth = (revenue_2024[-1] - revenue_2023[-1]) / revenue_2023[-1] * 100
ax.text(0.02, 0.95, f'YoY Growth: +{yoy_growth:.1f}%',
        transform=ax.transAxes, fontsize=12, color='green',
        fontweight='bold', va='top')

ax.set_title('Annual Revenue Comparison — 2023 vs 2024', fontsize=14, fontweight='bold')
ax.set_xlabel('Month', fontsize=12)
ax.set_ylabel('Revenue ($)', fontsize=12)
ax.yaxis.set_major_formatter(plt.FuncFormatter(lambda x, _: f'${x/1000:.0f}K'))
ax.legend(fontsize=11)
ax.grid(True, alpha=0.3)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.tight_layout()
plt.savefig('sales_comparison.png', dpi=150, bbox_inches='tight')
plt.show()

6. Common Mistakes

  • Forgetting tightlayout(): Without it, titles and labels overlap. Always call plt.tightlayout() before savefig().
  • plt.show() before savefig(): 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().

10. Next Chapter Recommendation

In Chapter 5: Line Charts and Trend Visualization, we master the most common business chart — visualizing revenue, website traffic, and KPI trends over time.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·