← Back to Glossary

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

Copy code

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.

Copy code

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.

Related terms

FAQS

No. Matplotlib works directly with plain Python lists and NumPy arrays. pandas DataFrames add a convenient .plot() method that calls Matplotlib under the hood, but it isn't required.