Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 923d8fb

Browse files
authored
Merge pull request #26610 from meeseeksmachine/auto-backport-of-pr-26538-on-v3.7.x
Backport PR #26538 on branch v3.7.x (Resolves #26421 Added an example for fig comparison decorator)
2 parents b7dfdc5 + 84421cc commit 923d8fb

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

doc/devel/testing.rst

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ case :file:`lib/matplotlib/tests/baseline_images/test_lines`). Put this new
122122
file under source code revision control (with ``git add``). When rerunning
123123
the tests, they should now pass.
124124

125+
It is preferred that new tests use ``style='mpl20'`` as this leads to smaller
126+
figures and reflects the newer look of default Matplotlib plots. Also, if the
127+
texts (labels, tick labels, etc) are not really part of what is tested, use
128+
``remove_text=True`` as this will lead to smaller figures and reduce possible
129+
issues with font mismatch on different platforms.
130+
125131
Baseline images take a lot of space in the Matplotlib repository.
126132
An alternative approach for image comparison tests is to use the
127133
`~matplotlib.testing.decorators.check_figures_equal` decorator, which should be
@@ -130,11 +136,24 @@ images on the figures using two different methods (the tested method and the
130136
baseline method). The decorator will arrange for setting up the figures and
131137
then collect the drawn results and compare them.
132138

133-
It is preferred that new tests use ``style='mpl20'`` as this leads to smaller
134-
figures and reflects the newer look of default Matplotlib plots. Also, if the
135-
texts (labels, tick labels, etc) are not really part of what is tested, use
136-
``remove_text=True`` as this will lead to smaller figures and reduce possible
137-
issues with font mismatch on different platforms.
139+
For example, this test compares two different methods to draw the same
140+
circle: plotting a circle using a `matplotlib.patches.Circle` patch
141+
vs plotting the circle using the parametric equation of a circle ::
142+
143+
from matplotlib.testing.decorators import check_figures_equal
144+
import matplotib.patches as mpatches
145+
import matplotlib.pyplot as plt
146+
import numpy as np
147+
148+
@check_figures_equal(extensions=['png'], tol=100)
149+
def test_parametric_circle_plot(fig_test, fig_ref):
150+
red_circle_ref = mpatches.Circle((0, 0), 0.2, color='r', clip_on=False)
151+
fig_ref.add_artist(red_circle_ref)
152+
theta = np.linspace(0, 2 * np.pi, 150)
153+
radius = 0.4
154+
fig_test.plot(radius * np.cos(theta), radius * np.sin(theta), color='r')
155+
156+
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.
138157

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

0 commit comments

Comments
 (0)