pytest-mpl

0.19.0 · active · verified Mon Apr 13

pytest-mpl is a pytest plugin designed to facilitate image comparison for Matplotlib figures. It enables users to test their Matplotlib outputs by comparing generated images against reference images using root mean square (RMS) difference or against SHA-256 hashes. The library is actively maintained, with its latest version being 0.19.0, and typically sees a few releases per year.

Warnings

Install

Imports

Quickstart

This example defines a basic pytest function that creates a Matplotlib figure and returns it. The `@pytest.mark.mpl_image_compare` decorator instructs pytest-mpl to compare the generated figure against a baseline. The commands below the code show how to generate initial baseline images and how to run the comparison tests. Ensure a directory named 'baseline_images' (or your chosen path) exists next to your test file.

import matplotlib.pyplot as plt
import pytest

@pytest.mark.mpl_image_compare
def test_simple_plot():
    fig, ax = plt.subplots()
    ax.plot([1, 2, 3], [4, 5, 6])
    ax.set_title("A simple line plot")
    return fig

# To generate baseline images (run once or when figures change):
# pytest --mpl-generate-path=baseline_images

# To run tests and compare against baselines:
# pytest --mpl

view raw JSON →