LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 819 min read

Static Visualization Libraries (Matplotlib & Seaborn)

Learn Matplotlib for foundational, fully customizable plotting and Seaborn for statistical visualization with polished defaults.

Introduction

Matplotlib is the foundational plotting library in the Python ecosystem — nearly every other visualization library, including Seaborn, either builds on it or was influenced by its API. Seaborn wraps Matplotlib with statistically aware chart types and much better default styling, trading some low-level control for speed of producing a polished chart.

What You Will Learn
  • What Matplotlib solves and how to build a basic line/bar chart with it.
  • What Seaborn solves and how to build a statistical chart like a boxplot or heatmap.
  • When to reach for raw Matplotlib versus Seaborn's higher-level API.

Matplotlib: Foundational Plotting

Use case: Matplotlib produces static, publication-quality charts with fine-grained control over every element — axes, ticks, colors, annotations. It is the lowest-level and most flexible plotting library in the ecosystem, which makes it the right choice whenever a chart needs precise, non-default customization.

pip install matplotlib

Matplotlib in Action

import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
revenue = [12000, 15500, 14200, 18900, 21300, 19800]
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(months, revenue, marker='o', color='#2563eb', linewidth=2)
ax.set_title('Monthly Revenue (H1)')
ax.set_xlabel('Month')
ax.set_ylabel('Revenue (USD)')
ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig('revenue_chart.png', dpi=150)
print("Chart saved to revenue_chart.png")
Terminal Output

Click Run to see what this code prints.

Figure and Axes

The fig, ax = plt.subplots() pattern is the standard, recommended way to use Matplotlib: fig represents the whole image, and ax represents one set of axes to draw on. It scales cleanly to multi-panel charts, which the simpler plt.plot() shortcut does not.

Seaborn: Statistical Visualization

Use case: Seaborn is built directly on top of Matplotlib and specializes in statistical charts — distributions, correlations, and category comparisons — with attractive defaults that would take significant manual Matplotlib code to replicate. It also integrates directly with pandas DataFrames, accepting column names instead of raw arrays.

pip install seaborn

Seaborn in Action

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
'department': ['Sales', 'Sales', 'Engineering', 'Engineering', 'Marketing', 'Marketing'],
'salary': [62000, 68000, 95000, 102000, 58000, 61000],
})
fig, axes = plt.subplots(1, 2, figsize=(10, 4))
sns.boxplot(data=df, x='department', y='salary', ax=axes[0])
axes[0].set_title('Salary Distribution by Department')
corr = df.assign(dept_code=df['department'].astype('category').cat.codes)[['salary', 'dept_code']].corr()
sns.heatmap(corr, annot=True, cmap='Blues', ax=axes[1])
axes[1].set_title('Correlation Heatmap')
plt.tight_layout()
plt.savefig('seaborn_charts.png', dpi=150)
print("Charts saved to seaborn_charts.png")
Terminal Output

Click Run to see what this code prints.

When to Use Matplotlib vs Seaborn

SituationReach For
Need a fully custom, pixel-precise chartMatplotlib
Need a quick, good-looking statistical chartSeaborn
Comparing distributions across categoriesSeaborn (boxplot, violinplot)
Visualizing correlation between many variablesSeaborn (heatmap, pairplot)
Building a chart type Seaborn does not offerMatplotlib, or Seaborn output further customized with Matplotlib

In practice these are not competitors: Seaborn charts are Matplotlib figures under the hood, so you can always drop down to Matplotlib's ax methods to fine-tune a Seaborn plot's title, labels, or styling.

Common Mistakes

Avoid These Mistakes
  • Manually recreating a boxplot or heatmap in raw Matplotlib when Seaborn already provides it in one line.
  • Forgetting plt.tight_layout() and ending up with cut-off axis labels in saved charts.
  • Passing raw NumPy arrays into Seaborn functions built for DataFrame column names, losing the automatic axis labeling.

Best Practices

  • Default to Seaborn for statistical exploration, and drop to Matplotlib only when you need precise control Seaborn does not expose.
  • Always title and label axes explicitly — do not rely on default variable names to communicate what a chart shows.
  • Save charts at a reasonable DPI (150 or higher) for anything going into a report or presentation.

Frequently Asked Questions

A little — Seaborn returns Matplotlib Axes objects, so you will eventually need basic Matplotlib knowledge to fine-tune titles, labels, or legends.

No — it is still actively maintained and remains the standard for full customization, even as newer libraries build on top of it.

No, both Matplotlib and Seaborn produce static images. Lesson 9 covers Plotly and Streamlit for interactive charts and dashboards.

Key Takeaways

  • Matplotlib is the foundational, fully customizable plotting library that most others build on.
  • Seaborn builds on Matplotlib to offer statistical chart types with better defaults and native pandas support.
  • They are complementary, not competing — Seaborn output can always be fine-tuned with Matplotlib.

Summary

Matplotlib and Seaborn cover the vast majority of static chart needs in data science: Matplotlib when you need full control, Seaborn when you need a fast, statistically sound chart with good defaults.

Lesson 8 Completed
  • You can build a basic Matplotlib chart.
  • You can build a Seaborn boxplot and heatmap.
  • You know when to reach for each library.
Next Lesson →

Interactive Visualization & Dashboards (Plotly & Streamlit)