Topic 03 / 8

Data Visualization with Matplotlib & Seaborn

~15 min read  //  Data Science Series  //  Coding India

Why Visualize?

Anscombe’s quartet famously demonstrated that datasets with identical summary statistics can look drastically different when graphed. Visualization is crucial for spotting trends, outliers, and patterns that numbers alone hide.

Matplotlib: The Foundation

Matplotlib is the workhorse of Python plotting. It gives you fine-grained control over every element of a figure.

import matplotlib.pyplot as plt

plt.plot(df["date"], df["revenue"])
plt.title("Revenue Over Time")
plt.xlabel("Date")
plt.ylabel("Revenue ($)")
plt.show()

Seaborn: Statistical Visualization

Seaborn is built on top of Matplotlib and provides a high-level interface for drawing attractive statistical graphics. It works seamlessly with Pandas DataFrames.

import seaborn as sns

# Scatter plot with a regression line
sns.regplot(x="marketing_spend", y="revenue", data=df)

# Box plot to show distribution across categories
sns.boxplot(x="category", y="revenue", data=df)

Choosing the Right Chart

  • Line Chart: Trends over time.
  • Bar Chart: Comparing categories.
  • Scatter Plot: Relationship between two continuous variables.
  • Histogram: Distribution of a single variable.
  • Box Plot: Distribution, median, and outliers across categories.