Skip to content

check_figures_equal does not account for changed background colors in comparison #16275

Closed
@andrewzwicky

Description

@andrewzwicky

Bug report

Bug summary

When check_figures_equal is used as a decorator, the default behavior is to remove any background color during savefig, so plots which should have a different background color can't be properly compared.

For example, code that should produce a figure with a black background and the reference figure would both be saved as a white background.

Code for reproduction

from matplotlib.testing.decorators import check_figures_equal

def create_test_figure_black_background(fig):
    axis = axis = fig.subplots()
    fig.set_facecolor('black')
    axis.bar(
        [0, 1, 2, 3, 4],
        [4, 5, 7, 9, 2],
        color=["red", "blue", "black", "yellow", "white"],
        edgecolor="black",
    )


@check_figures_equal(extensions=["png"])
def test_dark_background(test_figure, reference_figure):

    ref_ax = reference_figure.subplots()
    ref_ax.bar(
        [0, 1, 2, 3, 4],
        [4, 5, 7, 9, 2],
        color=["red", "blue", "black", "yellow", "white"],
        edgecolor="black",
    )

    create_test_figure_black_background(test_figure)

Actual outcome

running with pytest shows a passing test, even though the test_fig should have a black background and the reference figure has a white background. These are both changed to white in savefig

C:\Users\Andrew\Workspace\TripleAgent> pytest .\triple_agent\ -k test_dark_background
================================================================ test session starts =================================================================
platform win32 -- Python 3.7.5, pytest-4.5.0, py-1.8.0, pluggy-0.9.0
plugins: xdist-1.29.0, forked-1.0.2, cov-2.7.1
collected 1112 items / 1111 deselected / 1 selected

triple_agent\tests\test_plot_types.py .                                                                                                         [100%]

===================================================== 1 passed, 1111 deselected in 2.38 seconds ======================================================

Expected outcome

Expect that backgrounds are preserved so that functionality for backgrounds can be tested.

#15111 would likely solve the issue.

Matplotlib version

  • Operating system: Win10
  • Matplotlib version: 3.1.0
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg (i think?)
  • Python version: 3.7.5
  • Jupyter version (if applicable):
  • Other libraries:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions