pytest-mpl
pytest plugin to help with testing figures output from Matplotlib
What it is and what it does
pytest-mpl bridges the gap between Matplotlib figure generation and automated testing by letting you write test functions that return figures, then comparing their rendered output against stored reference images. It handles the visual regression workflow: generate a baseline set of reference images once, then on subsequent test runs, render new figures and measure the difference (either by RMS of pixel residuals or by hash). If the difference exceeds your tolerance threshold, the test fails, catching unintended changes to plots.
The plugin integrates directly into pytest's test discovery and marking system. You decorate test functions with `@pytest.mark.mpl_image_compare`, and pytest-mpl intercepts them, renders the returned figure to an image, and compares it. It depends on pytest for the test framework, matplotlib for rendering, Pillow for image I/O, Jinja2 for HTML report generation, and packaging for version handling. It's designed for projects that need to ensure plot outputs remain visually consistent across code changes.
Use it for:
- Catch unintended changes to scientific plots or data visualizations in CI/CD pipelines
- Verify that refactored plotting code produces identical visual output before and after
- Maintain visual regression baselines for library documentation or gallery examples
- Test custom Matplotlib extensions or themes to ensure consistent rendering across versions
- Validate plot appearance across different environments or backend configurations
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
pytest-mpl is a pytest plugin that automates visual regression testing for Matplotlib figures by comparing generated images against reference baselines using RMS tolerance or hash comparison.
Yes, if you maintain Matplotlib-heavy code and need automated visual regression testing. The plugin is actively maintained, has no known vulnerabilities, installs with low friction, and integrates seamlessly into pytest workflows. The BSD-2-Clause license is permissive. Install it if visual consistency of plots is part of your test suite; skip it if you don't generate or test figures programmatically.
Install
pytest-mpl on PyPI
pip
pip install pytest-mpluv
uv add pytest-mplpoetry
poetry add pytest-mplInstalling pytest-mpl
Before you install
Low friction installation with a pure-Python wheel. Active maintenance (last commit 2026-07-01) and stable status since early releases (2015). Depends on pytest, matplotlib, and common utilities—all widely used and well-maintained.
License in practice
BSD-2-Clause is permissive; you can use this in commercial or proprietary projects with minimal restrictions beyond retaining the license notice.
Quickstart
pip install pytest-mpl
import matplotlib.pyplot as plt
import pytest
@pytest.mark.mpl_image_compare
def test_plot():
fig, ax = plt.subplots()
ax.plot([1, 2])
return fig
# Generate baseline: pytest --mpl-generate-path=baseline
# Run tests: pytest --mpl
Requires Python >= 3.10. Matplotlib must be installed and functional in your environment.
Verify before relying
- Exact tolerance thresholds and RMS calculation method for image comparison
- Performance impact when testing large numbers of figures or high-resolution images
- Compatibility with different Matplotlib backends and rendering engines
Package facts
| License | BSD-2-Clause (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 5 — pytest, matplotlib, packaging, Jinja2, Pillow |
| Maintenance | actively maintained — 142 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 677,495/month — #5,380 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pytest_mpl-0.19.0-py3-none-any.whl
Tags
More Testing packages
Pluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
virtualenvvirtualenv creates isolated Python environments…
permissive · top 100 on PyPI
coverageCoverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
pytest-asynciopytest-asyncio is a pytest plugin that enables…
permissive · top 1,000 on PyPI
pytest-json-ctrfA pytest plugin that generates test reports in…
permissive · top 1,000 on PyPI
pytest-regressionsProvides pytest fixtures for writing regression…
permissive · top 5,000 on PyPI
pytest-playwright-visualA pytest plugin that captures and compares…
permissive · top 15,000 on PyPI
matplotlibmatplotlib creates static, animated, and…
permissive · top 1,000 on PyPI
matplotlib-inlineEnables matplotlib figures to display inline…
permissive · top 1,000 on PyPI
pixelhogCompares PNG screenshots pixel-by-pixel and…
permissive · top 5,000 on PyPI
pytest-textual-snapshotA pytest plugin that captures SVG screenshots…
permissive · top 15,000 on PyPI
lovelyplotsLovelyPlots provides matplotlib style sheets…
permissive · top 15,000 on PyPI
pytest-arraydiffA pytest plugin that generates and compares…
permissive · top 15,000 on PyPI
plotly-resamplerAdds dynamic data aggregation to Plotly…
permissive · top 15,000 on PyPI
imgcatDisplays images in iTerm2, WezTerm, and tmux by…
permissive · top 15,000 on PyPI