Skip to content

Resolves #26421 Added an example for fig comparison decorator #26538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Aug 28, 2023
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9459e42
Update testing.rst
SunSummoner Aug 15, 2023
e6229e2
Merge pull request #1 from SunSummoner/SunSummoner-patch-1
SunSummoner Aug 15, 2023
1c28caa
Update testing.rst
SunSummoner Aug 16, 2023
4df929f
Merge branch 'matplotlib:main' into main
SunSummoner Aug 16, 2023
0ff2125
Update doc/devel/testing.rst
SunSummoner Aug 16, 2023
a182f3f
Update testing.rst
SunSummoner Aug 17, 2023
f1726fc
Update testing.rst
SunSummoner Aug 17, 2023
4edea20
Update testing.rst
SunSummoner Aug 17, 2023
302aa4e
Update doc/devel/testing.rst
SunSummoner Aug 17, 2023
bb3a00e
Update testing.rst
SunSummoner Aug 17, 2023
30bb251
Merge branch 'matplotlib:main' into main
SunSummoner Aug 18, 2023
6d2e74f
Resolving pre-commit-hook changes
SunSummoner Aug 18, 2023
1b74005
Merge branch 'matplotlib:main' into main
SunSummoner Aug 18, 2023
412f8c3
Update doc/devel/testing.rst
SunSummoner Aug 19, 2023
132b13d
Resolving pre-commit-hook changes
SunSummoner Aug 19, 2023
9d5ea74
Merge branch 'main' of https://github.com/SunSummoner/matplotlib
SunSummoner Aug 19, 2023
744e6d4
Resolving pre-commit-hook changes
SunSummoner Aug 22, 2023
175ebd8
Merge branch 'matplotlib:main' into main
SunSummoner Aug 22, 2023
461494e
Update testing.rst
SunSummoner Aug 22, 2023
4add791
Update testing.rst
SunSummoner Aug 22, 2023
02203f1
Update testing.rst
SunSummoner Aug 22, 2023
4d7c58f
Update testing.rst
SunSummoner Aug 22, 2023
323fd7b
Update testing.rst
SunSummoner Aug 22, 2023
8e51433
Update testing.rst
SunSummoner Aug 22, 2023
30251e3
Update doc/devel/testing.rst
SunSummoner Aug 23, 2023
3d74be9
Update doc/devel/testing.rst
SunSummoner Aug 23, 2023
1e94e0c
Merge branch 'matplotlib:main' into main
SunSummoner Aug 27, 2023
c386d8e
accidental new line
story645 Aug 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions doc/devel/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ case :file:`lib/matplotlib/tests/baseline_images/test_lines`). Put this new
file under source code revision control (with ``git add``). When rerunning
the tests, they should now pass.

It is preferred that new tests use ``style='mpl20'`` as this leads to smaller
figures and reflects the newer look of default Matplotlib plots. Also, if the
texts (labels, tick labels, etc) are not really part of what is tested, use
``remove_text=True`` as this will lead to smaller figures and reduce possible
issues with font mismatch on different platforms.

Baseline images take a lot of space in the Matplotlib repository.
An alternative approach for image comparison tests is to use the
`~matplotlib.testing.decorators.check_figures_equal` decorator, which should be
Expand All @@ -130,11 +136,24 @@ images on the figures using two different methods (the tested method and the
baseline method). The decorator will arrange for setting up the figures and
then collect the drawn results and compare them.

It is preferred that new tests use ``style='mpl20'`` as this leads to smaller
figures and reflects the newer look of default Matplotlib plots. Also, if the
texts (labels, tick labels, etc) are not really part of what is tested, use
``remove_text=True`` as this will lead to smaller figures and reduce possible
issues with font mismatch on different platforms.
For example, this test compares two different methods to draw the same
circle: plotting a circle using a `matplotlib.patches.Circle` patch
vs plotting the circle using the parametric equation of a circle ::

from matplotlib.testing.decorators import check_figures_equal
import matplotib.patches as mpatches
import matplotlib.pyplot as plt
import numpy as np

@check_figures_equal(extensions=['png'], tol=100)
def test_parametric_circle_plot(fig_test, fig_ref):
red_circle_ref = mpatches.Circle((0, 0), 0.2, color='r', clip_on=False)
fig_ref.add_artist(red_circle_ref)
theta = np.linspace(0, 2 * np.pi, 150)
radius = 0.4
fig_test.plot(radius * np.cos(theta), radius * np.sin(theta), color='r')

Both comparison decorators have a tolerance argument ``tol`` that is used to specify the tolerance for difference in color value between the two images, where 255 is the maximal difference. The test fails if the average pixel difference is greater than this value.

See the documentation of `~matplotlib.testing.decorators.image_comparison` and
`~matplotlib.testing.decorators.check_figures_equal` for additional information
Expand Down