@@ -143,60 +143,40 @@ The seed is John Hunter's birthday.
143143Writing an image comparison test
144144--------------------------------
145145
146- Writing an image based test is only slightly more difficult than a
147- simple test. The main consideration is that you must specify the
148- "baseline", or expected, images in the
149- :func: `~matplotlib.testing.decorators.image_comparison ` decorator. For
150- example, this test generates a single image and automatically tests it::
151-
152- import numpy as np
153- import matplotlib
154- from matplotlib.testing.decorators import image_comparison
155- import matplotlib.pyplot as plt
156-
157- @image_comparison(baseline_images=['spines_axes_positions'],
158- extensions=['png'])
159- def test_spines_axes_positions():
160- # SF bug 2852168
161- fig = plt.figure()
162- x = np.linspace(0,2*np.pi,100)
163- y = 2*np.sin(x)
164- ax = fig.add_subplot(1,1,1)
165- ax.set_title('centered spines')
166- ax.plot(x,y)
167- ax.spines['right'].set_position(('axes',0.1))
168- ax.yaxis.set_ticks_position('right')
169- ax.spines['top'].set_position(('axes',0.25))
170- ax.xaxis.set_ticks_position('top')
171- ax.spines['left'].set_color('none')
172- ax.spines['bottom'].set_color('none')
173-
174- The first time this test is run, there will be no baseline image to
175- compare against, so the test will fail. Copy the output images (in
176- this case `result_images/test_category/spines_axes_positions.png `) to
177- the correct subdirectory of `baseline_images ` tree in the source
178- directory (in this case
179- `lib/matplotlib/tests/baseline_images/test_category `). Put this new
180- file under source code revision control (with `git add `). When
181- rerunning the tests, they should now pass.
182-
183- The :func: `~matplotlib.testing.decorators.image_comparison ` decorator
184- defaults to generating ``png ``, ``pdf `` and ``svg `` output, but in
185- interest of keeping the size of the library from ballooning we should only
186- include the ``svg `` or ``pdf `` outputs if the test is explicitly exercising
187- a feature dependent on that backend.
188-
189- There are two optional keyword arguments to the `image_comparison `
190- decorator:
191-
192- - `extensions `: If you only wish to test additional image formats (rather than
193- just `png `), pass any additional file types in the list of the extensions to
194- test. When copying the new baseline files be sure to only copy the output
195- files, not their conversions to ``png ``. For example only copy the files
196- ending in ``pdf ``, not in ``_pdf.png ``.
197-
198- - `tol `: This is the image matching tolerance, the default `1e-3 `. If some
199- variation is expected in the image between runs, this value may be adjusted.
146+ Writing an image-based test is only slightly more difficult than a simple
147+ test. The main consideration is that you must specify the "baseline", or
148+ expected, images in the `~matplotlib.testing.decorators.image_comparison `
149+ decorator. For example, this test generates a single image and automatically
150+ tests it::
151+
152+ from matplotlib.testing.decorators import image_comparison
153+ import matplotlib.pyplot as plt
154+
155+ @image_comparison(baseline_images=['line_dashes'], remove_text=True,
156+ extensions=['png'])
157+ def test_line_dashes():
158+ fig, ax = plt.subplots()
159+ ax.plot(range(10), linestyle=(0, (3, 3)), lw=5)
160+
161+ The first time this test is run, there will be no baseline image to compare
162+ against, so the test will fail. Copy the output images (in this case
163+ :file: `result_images/test_lines/test_line_dashes.png `) to the correct
164+ subdirectory of :file: `baseline_images ` tree in the source directory (in this
165+ case :file: `lib/matplotlib/tests/baseline_images/test_lines `). Put this new
166+ file under source code revision control (with ``git add ``). When rerunning
167+ the tests, they should now pass.
168+
169+ Baseline images take a lot of space in the Matplotlib repository.
170+ An alternative approach for image comparison tests is to use the
171+ `~matplotlib.testing.decorators.check_figures_equal ` decorator, which should be
172+ used to decorate a function taking two `Figure ` parameters and draws the same
173+ images on the figures using two different methods (the tested method and the
174+ baseline method). The decorator will arrange for setting up the figures and
175+ then collect the drawn results and compare them.
176+
177+ See the documentation of `~matplotlib.testing.decorators.image_comparison ` and
178+ `~matplotlib.testing.decorators.check_figures_equal ` for additional information
179+ about their use.
200180
201181Known failing tests
202182-------------------
0 commit comments