| [ Web Proxy ] |
| Viewing: https://matplotlib.org/stable/api/_as_gen/../testing_api.html | [Back] [Original] |
matplotlib.testing#matplotlib.testing#Helper functions for testing.
Create and run a subprocess.
Thin wrapper around subprocess.run, intended for testing. Will
mark fork() failures on Cygwin as expected failures: not a
success, but not indicating a problem with the code either.
Also called universal_newlines in subprocess. I chose this
name since the main effect is returning bytes (False) vs. str
(True), though it also tries to normalize newlines across
platforms.
Set stdout and stderr to subprocess.PIPE
If running on emscripten, which does not support subprocesses.
If platform is Cygwin and subprocess reports a fork() failure.
See also
Run a function in a sub-process.
The function to be run. It must be in a module that is importable.
Any additional command line arguments to be passed in
the first argument to subprocess.run.
Any additional environment variables to be set for the subprocess.
Replace texts with placeholder rectangles.
The rectangle size only depends on the font size and the number of characters. It is thus insensitive to font properties and rendering details. This should be used for tests that depend on text geometries but not the actual text rendering, e.g. layout tests.
matplotlib.testing.compare#Utilities for comparing image results.
Calculate the per-pixel errors, then compute the root mean square error.
Return the list of file formats that compare_images can compare
on this system.
E.g. ['png', 'pdf', 'svg', 'eps'].
Compare two "image" files checking differences within a tolerance.
The two given filenames may point to files which are convertible to
PNG via the converter dictionary. The underlying RMS is calculated
in a similar way to the calculate_rms function.
The filename of the expected image.
The filename of the actual image.
The tolerance (a color value difference, where 255 is the maximal difference). The test fails if the average pixel difference is greater than this value.
Determines the output format. If called from image_comparison decorator, this should be True. (default=False)
Return None if the images are equal within the given tolerance.
If the images differ, the return value depends on in_decorator. If in_decorator is true, a dict with the following entries is returned:
rms: The RMS of the image difference.
expected: The filename of the expected image.
actual: The filename of the actual image.
diff_image: The filename of the difference image.
tol: The comparison tolerance.
Otherwise, a human-readable multi-line string representation of this information is returned.
Examples
img1 = "./baseline/plot.png"
img2 = "./output/plot.png"
compare_images(img1, img2, 0.001)
matplotlib.testing.decorators#Decorator for test cases that generate and compare two figures.
The decorated function must take two keyword arguments, fig_test and fig_ref, and draw the test and reference images on them. After the function returns, the figures are saved and compared.
This decorator should be preferred over image_comparison when possible in
order to keep the size of the test suite from ballooning.
The extensions to test. Supported extensions are "png", "pdf", "svg".
Testing with the one default extension is sufficient if the output is not
format dependent, e.g. if you test that a bar() plot yields the same
result as some manually placed Rectangles. You should use all extensions
if a renderer property is involved, e.g. correct alpha blending.
The RMS threshold above which the test is considered failed.
If any new figures are created (and not subsequently closed) inside the test function.
Examples
Check that calling Axes.plot with a single argument plots it against
[0, 1, 2, ...]:
@check_figures_equal()
def test_plot(fig_test, fig_ref):
fig_test.subplots().plot([1, 3, 5])
fig_ref.subplots().plot([0, 1, 2], [1, 3, 5])
Compare images generated by the test with those specified in
baseline_images, which must correspond, else an ImageComparisonFailure
exception will be raised.
A list of strings specifying the names of the images generated by
calls to Figure.savefig.
If None, the test function must use the baseline_images fixture,
either as a parameter or with pytest.mark.usefixtures. This value is
only allowed when using pytest.
The list of extensions to test, e.g. ['png', 'pdf'].
If None, defaults to: png, pdf, and svg.
When testing a single extension, it can be directly included in the names passed to baseline_images. In that case, extensions must not be set.
In order to keep the size of the test suite from ballooning, we only
include the svg or pdf outputs if the test is explicitly
exercising a feature dependent on that backend (see also the
check_figures_equal decorator for that purpose).
The RMS threshold above which the test is considered failed.
Due to expected small differences in floating-point calculations, on 32-bit systems an additional 0.06 is added to this threshold.
The expected freetype version or range of versions for this test to pass.
Remove the title and tick text from the figure before comparison. This is useful to make the baseline images independent of variations in text rendering between different versions of FreeType.
This does not remove other, more deliberate, text, such as legends and annotations.
Optional arguments that are passed to the savefig method.
The style(s) to apply to the image test. The test itself can also apply additional styles if desired.
Changed in version 3.11: This defaults to ['classic', '_classic_test_patch'], but will be
changing to 'mpl20' as of Matplotlib 3.13. A warning is raised if not
explicitly passed.
matplotlib.testing.exceptions#Bases: AssertionError
Raise this exception to mark a test as a comparison between two images.
For more information on fixtures, see pytest fixtures.
Fixture to import and configure pandas. Using this fixture, the test is skipped when pandas is not installed. Use this fixture instead of importing pandas in test files.
Examples
Request the pandas fixture by passing in pd as an argument to the test
def test_matshow_pandas(pd):
df = pd.DataFrame({'x':[1,2,3], 'y':[4,5,6]})
im = plt.figure().subplots().matshow(df)
np.testing.assert_array_equal(im.get_array(), df)
Fixture to import xarray so that the test is skipped when xarray is not installed. Use this fixture instead of importing xrray in test files.
Examples
Request the xarray fixture by passing in xr as an argument to the test
def test_imshow_xarray(xr):
ds = xr.DataArray(np.random.randn(2, 3))
im = plt.figure().subplots().imshow(ds)
np.testing.assert_array_equal(im.get_array(), ds)
| Web Proxy Viewer | New URL | Original Page |