matplotlib/matplotlib · error · ImageComparisonFailure

Image sizes do not match expected size: {expected_image.shap

Error message

Image sizes do not match expected size: {expected_image.shape} actual size {actual_image.shape}

What it means

Error "Image sizes do not match expected size: {expected_image.shape} actual size {actual_image.shape}" thrown in matplotlib/matplotlib.

Source

Thrown at lib/matplotlib/testing/compare.py:391


def crop_to_same(actual_path, actual_image, expected_path, expected_image):
    # clip the images to the same size -- this is useful only when
    # comparing eps to pdf
    if actual_path[-7:-4] == 'eps' and expected_path[-7:-4] == 'pdf':
        aw, ah, ad = actual_image.shape
        ew, eh, ed = expected_image.shape
        actual_image = actual_image[int(aw / 2 - ew / 2):int(
            aw / 2 + ew / 2), int(ah / 2 - eh / 2):int(ah / 2 + eh / 2)]
    return actual_image, expected_image


def calculate_rms(expected_image, actual_image):
    """
    Calculate the per-pixel errors, then compute the root mean square error.
    """
    if expected_image.shape != actual_image.shape:
        raise ImageComparisonFailure(
            f"Image sizes do not match expected size: {expected_image.shape} "
            f"actual size {actual_image.shape}")
    # Convert to float to avoid overflowing finite integer types.
    return np.sqrt(((expected_image - actual_image).astype(float) ** 2).mean())


# NOTE: compare_image and save_diff_image assume that the image does not have
# 16-bit depth, as Pillow converts these to RGB incorrectly.


def _load_image(path):
    img = Image.open(path)
    if img.mode != "RGB":
        # If we have anything other than RGB(A) (e.g., a paletted image), convert it
        # to RGBA.
        if img.mode != "RGBA":
            img = img.convert("RGBA")
        # In an RGBA image, if the smallest value in the alpha channel is 255, all

View on GitHub (pinned to b379c1b69e)

When it happens

Trigger: Thrown at lib/matplotlib/testing/compare.py:391 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of matplotlib/matplotlib@b379c1b69e (2026-08-21). Data as JSON: /api/errors/f0c093a2c94b748c. Report an issue: GitHub.