Free — no signup required

Visualizing for Debugging: Matplotlib & Seaborn

2 min read

Seeing is Believing

You cannot debug a dataset by reading numbers alone. A column of 10,000 values tells you almost nothing until you visualize its shape. Matplotlib is the foundational Python plotting library — it gives you precise control over every element of a chart. Seaborn is a high-level wrapper built on top of Matplotlib that produces statistically-oriented plots with far less code and better default aesthetics.

Think of Matplotlib as the raw canvas and brushes, and Seaborn as a set of pre-built stencils for the most common data science plots.

1. The Histogram — Distribution Check

Question: "What does the spread of my data look like? Do I have outliers? Is it normally distributed?"

import seaborn as sns
import matplotlib.pyplot as plt

sns.histplot(df['age'], bins=30, kde=True)
plt.title("Distribution of Age")
plt.show()

What to look for:
- A bar at Age = 200 means dirty data — a typo or a sentinel value.
- A heavily right-skewed distribution (long tail to the right) means most values are low but a few are extremely high. Many ML models assume normality; you may need a log transform.
- A bimodal distribution (two humps) suggests your data contains two distinct populations that should be modeled separately.

2. The Scatter Plot — Relationship Check

Question: "Is Feature A correlated with Target B? Is the relationship linear?"

sns.scatterplot(x=df['square_feet'], y=df['price'], alpha=0.4)
plt.title("Square Footage vs. Price")
plt.show()

What to look for:
- A diagonal band of points indicates positive linear correlation — the feature is likely useful.
- A random cloud indicates no relationship — the feature may be noise.
- A curved pattern (e.g., exponential) indicates a non-linear relationship. A linear model will underfit; you may need polynomial features or a tree-based model.

3. The Heatmap — Correlation Matrix

Question: "Are any of my features redundant with each other (multicollinearity)?"

import pandas as pd

corr_matrix = df.corr()
sns.heatmap(corr_matrix, annot=True, fmt=".2f", cmap="coolwarm")
plt.title("Feature Correlation Matrix")
plt.show()

What to look for:
- A correlation of 1.0 or -1.0 between two features means they carry identical information. Drop one — keeping both confuses linear models and inflates feature importance scores.
- A correlation near 0.0 between a feature and your target variable is a signal (not proof) that the feature may not be useful.
- High inter-feature correlation (multicollinearity) specifically breaks the coefficient interpretability of linear regression, even if it doesn't always hurt predictive accuracy.

This is one of 18 chapters

Every chapter is free — Kubernetes, Terraform, SRE, distributed systems, and more — with fast daily review built in.

Sign up free