Skip to content

Commit 28c0a01

Browse files
committed
Added docs about deterministic figures
1 parent 28b171e commit 28c0a01

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

README.rst

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ When generating a hash library, the tests will also be run as usual against the
9090
existing hash library specified by ``--mpl-hash-library`` or the keyword argument.
9191
However, generating baseline images will always result in the tests being skipped.
9292

93-
9493
Hybrid Mode: Hashes and Images
9594
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9695

@@ -278,6 +277,62 @@ decorator:
278277
This will make the test insensitive to changes in e.g. the freetype
279278
library.
280279

280+
Supported formats and deterministic output
281+
------------------------------------------
282+
283+
By default, pytest-mpl will save and compare figures in PNG format. However,
284+
it is possible to set the format to use by setting e.g. ``savefig_kwargs={'format': 'pdf'}``
285+
in ``mpl_image_compare``. Supported formats are ``'eps'``, ``'pdf'``, ``'png'``, and ``'svg'``.
286+
Note that Ghostscript is required to be installed for comparing PDF and EPS figures, while
287+
Inkscape is required for SVG comparison.
288+
289+
By default, Matplotlib does not produce deterministic output that will
290+
have a consistent hash every time it is run, or over different Matplotlib versions.
291+
In order to enforce that the output is deterministic, you will need to do the following:
292+
293+
PNG
294+
^^^
295+
296+
For PNG files, the output can be made deterministic by setting::
297+
298+
@pytest.mark.mpl_image_compare(savefig_kwargs={'metadata': {"Software": None}})
299+
300+
PDF
301+
^^^
302+
303+
For PDF files, the output can be made deterministic by setting::
304+
305+
@pytest.mark.mpl_image_compare(savefig_kwargs={'format': 'pdf',
306+
'metadata': {"Creator": None,
307+
"Producer": None,
308+
"CreationDate": None}})
309+
310+
EPS
311+
^^^
312+
313+
For PDF files, the output can be made deterministic by setting::
314+
315+
@pytest.mark.mpl_image_compare(savefig_kwargs={'format': 'pdf',
316+
'metadata': {"Creator": "test"})
317+
318+
and in addition you will need to set the SOURCE_DATE_EPOCH environment variable to
319+
a constant value (this is a unit timestamp)::
320+
321+
os.environ['SOURCE_DATE_EPOCH'] = '1680254601'
322+
323+
You could do this inside the test.
324+
325+
SVG
326+
^^^
327+
328+
For SVG files, the output can be made deterministic by setting::
329+
330+
@pytest.mark.mpl_image_compare(savefig_kwargs={'metadata': '{"Date": None}})
331+
332+
and in addition, you should make sure the following rcParam is set to a constant string::
333+
334+
plt.rcParams['svg.hashsalt'] = 'test'
335+
281336
Test failure example
282337
--------------------
283338

tests/baseline/hashes/mpl20_ft261.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"test.test_modified": "54f6cf83d5b06fa2ecb7fa23d6e87898679178ef5d0dfdd2551a139f1932127b",
1414
"test.test_new": "54f6cf83d5b06fa2ecb7fa23d6e87898679178ef5d0dfdd2551a139f1932127b",
1515
"test.test_unmodified": "54f6cf83d5b06fa2ecb7fa23d6e87898679178ef5d0dfdd2551a139f1932127b",
16-
"test_formats.test_format_eps": "da67357559501ff4afae72acede897d75d968ba0c4573dcd7ff992cfa72e49d4",
17-
"test_formats.test_format_pdf": "9098ce368ec6e20951e9305fcb7da05a3f10169d270c84d8da3331371c6596bc",
16+
"test_formats.test_format_eps": "396d67b9a51519a312903e817211abef713151225deeb6735cca6059ace3ca4e",
17+
"test_formats.test_format_pdf": "a73ea1bd1a9edc68da280240a080c3b8ae11a3b1170d27f7229e95ba3019c098",
1818
"test_formats.test_format_png": "480062c2239ed9d70e361d1a5b578dc2aa756971161ac6e7287b492ae6118c59"
1919
}

tests/test_pytest_mpl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ def test_formats(pytester, use_hash_library, passes, file_format):
702702
elif file_format == 'pdf':
703703
metadata = '{"Creator": None, "Producer": None, "CreationDate": None}'
704704
elif file_format == 'eps':
705-
metadata = '{"Creator": None}'
705+
metadata = '{"Creator": "test"}'
706706
elif file_format == 'svg':
707707
metadata = '{"Date": None}'
708708

0 commit comments

Comments
 (0)