Data Visualization with Matplotlib and Seaborn
Create publication-quality charts — line plots, bar charts, scatter plots, heatmaps, and the principles of effective data visualization.
2 min read
Updated Feb 12, 2026
Reviewed Jan 30, 2026
Why Visualization Matters
Numbers in a table are abstract. A well-designed chart reveals patterns, outliers, and trends that would be invisible in raw data. Visualization is not decoration — it is analysis.
Matplotlib Basics
Matplotlib is Python’s foundational plotting library. Seaborn builds on top of it with statistical visualizations and better defaults.
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
# Basic line plot
x = np.linspace(0, 10, 100)
plt.figure(figsize=(10, 6))
plt.plot(x, np.sin(x), label="sin(x)")
plt.plot(x, np.cos(x), label="cos(x)")
plt.xlabel("x")
plt.ylabel("y")
plt.title("Trigonometric Functions")
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Common Chart Types
Bar Charts
categories = ["Python", "JavaScript", "Rust", "Go"]
values = [85, 78, 45, 52]
plt.figure(figsize=(8, 5))
plt.bar(categories, values, color=["#306998", "#f7df1e", "#ce412b", "#00add8"])
plt.title("Language Popularity")
plt.ylabel("Score")
plt.show()
Scatter Plots
df = pd.DataFrame({
"experience": np.random.uniform(0, 20, 100),
"salary": np.random.uniform(30000, 150000, 100),
"department": np.random.choice(["Engineering", "Marketing", "Sales"], 100)
})
sns.scatterplot(data=df, x="experience", y="salary", hue="department", alpha=0.7)
plt.title("Salary vs Experience")
plt.show()
Histograms and Distributions
data = np.random.normal(100, 15, 1000)
sns.histplot(data, kde=True, bins=30)
plt.title("Distribution of Scores")
plt.xlabel("Score")
plt.show()
Seaborn Statistical Plots
Heatmaps
# Correlation matrix
corr = df.select_dtypes(include="number").corr()
sns.heatmap(corr, annot=True, cmap="coolwarm", center=0)
plt.title("Feature Correlations")
plt.show()
Box Plots
sns.boxplot(data=df, x="department", y="salary")
plt.title("Salary Distribution by Department")
plt.show()
Principles of Effective Visualization
- Choose the right chart type — bar for comparison, line for trends, scatter for relationships
- Label everything — axes, title, legend. If someone cannot understand the chart without explanation, it needs more labels
- Remove clutter — no 3D effects, minimal gridlines, reasonable axis ranges
- Use color intentionally — highlight what matters, use consistent palettes
- Show the data — avoid chart types that obscure the underlying distribution (pie charts are almost always worse than bar charts)
Saving Figures
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x, y)
fig.savefig("chart.png", dpi=150, bbox_inches="tight")
fig.savefig("chart.svg") # Vector format for publications
Summary
Start with Seaborn for quick statistical plots and clean defaults. Drop to Matplotlib when you need fine-grained control. The best visualization is the one that answers a question clearly.