Back to blog
AI Systemsbeginner

Matplotlib Detailed Tutorial: From Basic Plots to Professional Visualizations

Learn Matplotlib with practical examples: line, bar, scatter, histogram, subplots, styling, annotations, and publishing-quality chart design.

Asma HafeezMay 6, 20262 min read
MatplotlibPythonData VisualizationChartsPlottingAnalyticsData Science
Share:𝕏

Matplotlib Detailed Tutorial

Matplotlib is the core plotting library in Python. Mastering it helps you communicate analysis clearly, not just compute numbers.


1) Setup

Bash
pip install matplotlib
Python
import matplotlib.pyplot as plt
import numpy as np

2) Line Plot

Python
x = np.arange(1, 8)
y = np.array([10, 14, 13, 18, 22, 20, 25])

plt.plot(x, y, marker="o", linewidth=2)
plt.title("Weekly Revenue")
plt.xlabel("Day")
plt.ylabel("Revenue")
plt.grid(alpha=0.3)
plt.show()

3) Bar and Horizontal Bar Charts

Python
labels = ["API", "Frontend", "Data", "DevOps"]
values = [45, 30, 55, 25]

plt.bar(labels, values, color="#4f46e5")
plt.title("Team Story Points")
plt.show()

Use barh() when labels are long and hard to read vertically.


4) Scatter Plot for Relationships

Python
x = np.random.normal(50, 10, 200)
y = x * 1.4 + np.random.normal(0, 8, 200)

plt.scatter(x, y, alpha=0.6)
plt.title("Ad Spend vs Revenue")
plt.xlabel("Ad Spend")
plt.ylabel("Revenue")
plt.show()

5) Histogram for Distributions

Python
latency = np.random.normal(120, 20, 1000)
plt.hist(latency, bins=30, color="#0ea5e9", edgecolor="white")
plt.title("API Latency Distribution")
plt.xlabel("Latency (ms)")
plt.ylabel("Count")
plt.show()

6) Subplots and Multi-Chart Dashboards

Python
fig, axes = plt.subplots(2, 2, figsize=(10, 6))
axes[0, 0].plot([1, 2, 3], [2, 4, 3])
axes[0, 1].bar(["A", "B", "C"], [4, 7, 5])
axes[1, 0].scatter([1, 2, 3], [3, 2, 5])
axes[1, 1].hist(np.random.randn(200), bins=20)
fig.suptitle("KPI Overview")
plt.tight_layout()
plt.show()

7) Styling for Production-Quality Charts

  • Keep backgrounds simple
  • Avoid too many colors
  • Use clear axis labels and titles
  • Highlight one important story
  • Add units (ms, $, %) consistently

Example:

Python
plt.style.use("seaborn-v0_8-whitegrid")

8) Annotations and Reference Lines

Python
plt.plot([1, 2, 3, 4], [50, 55, 53, 70])
plt.axhline(60, color="red", linestyle="--", label="Target")
plt.annotate("Spike", xy=(4, 70), xytext=(3.2, 74),
             arrowprops=dict(arrowstyle="->"))
plt.legend()
plt.show()

9) Save Figures Correctly

Python
plt.savefig("report_chart.png", dpi=300, bbox_inches="tight")

Use dpi=300 for reports/presentations.


10) Practical Mini Project

Build a monthly_sales_report.py script that:

  1. Reads a CSV with monthly sales
  2. Produces line + bar + distribution charts
  3. Saves all charts into an output/ folder
  4. Adds chart titles suitable for executives

11) Common Mistakes

  • No labels or unclear axes
  • Overloaded charts with too much data
  • Poor color contrast
  • Not using tight_layout()

Next Step

Run these workflows interactively in:

Enjoyed this article?

Explore the AI Systems learning path for more.

Found this helpful?

Share:𝕏

Leave a comment

Have a question, correction, or just found this helpful? Leave a note below.