---
title: "Matplotlib"
description: "Matplotlib is Python's foundational 2D plotting library, used to create static, animated, and interactive charts from arrays, lists, and DataFrames."
canonical: "https://motherduck.com/glossary/matplotlib/"
related:
  - title: "DuckDB vs Pandas vs Polars for Python Developers"
    url: "https://motherduck.com/blog/duckdb-versus-pandas-versus-polars/"
  - title: "PyPi Data | MotherDuck Docs"
    url: "https://motherduck.com/docs/getting-started/sample-data-queries/pypi/"
  - title: "DuckLake: The Definitive Guide — Live Author Q&A with Matt Martin & Alex Monahan | MotherDuck"
    url: "https://motherduck.com/videos/ducklake-definitive-guide-oreilly-book/"
---

# Matplotlib

> Matplotlib is Python's foundational 2D plotting library, used to create static, animated, and interactive charts from arrays, lists, and DataFrames.

## Overview

Matplotlib is the original and most widely used plotting library in the Python data ecosystem. It exposes both a high-level, MATLAB-style interface (`pyplot`) for quick charts and a lower-level, object-oriented API (`Figure`, `Axes`) for precise control over every element of a plot. Most other Python visualization libraries — pandas' built-in `.plot()`, seaborn, and many dashboarding tools — are built directly on top of Matplotlib or use it as a rendering backend.

## Basic usage

```python
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(dates, revenue, label="Revenue")
ax.set_xlabel("Date")
ax.set_ylabel("Revenue ($)")
ax.legend()
plt.savefig("revenue.png")
```

The object-oriented API (working with `fig` and `ax` explicitly) is generally recommended over the implicit `plt.plot()` shortcuts once a script needs more than a single simple chart, since it scales better to multi-panel figures and reusable plotting functions.

## Typical workflow with a query engine

Matplotlib doesn't query data itself — it plots whatever arrays or DataFrame columns you give it. A common pattern is to run aggregation and filtering in SQL against a fast engine like DuckDB, pull back a small, already-summarized result, and hand that off to Matplotlib for charting, rather than loading a full raw dataset into memory just to plot a handful of aggregate values.

```python
import duckdb
import matplotlib.pyplot as plt

df = duckdb.sql(
    "SELECT date_trunc('month', order_date) AS month, sum(amount) AS revenue "
    "FROM read_parquet('orders.parquet') GROUP BY ALL ORDER BY ALL"
).df()

plt.plot(df["month"], df["revenue"])
plt.show()
```

This keeps the heavy lifting in the query engine and leaves Matplotlib to do what it's good at: rendering the final chart.
