matplotlib.testing#

matplotlib.testing#

用于测试的辅助函数。

matplotlib.testing.ipython_in_subprocess(requested_backend_or_gui_framework, all_expected_backends)[source]#
matplotlib.testing.is_ci_environment()[source]#
matplotlib.testing.set_font_settings_for_testing()[source]#
matplotlib.testing.set_reproducibility_for_testing()[source]#
matplotlib.testing.setup()[source]#
matplotlib.testing.subprocess_run_for_testing(command, env=None, timeout=60, stdout=None, stderr=None, check=False, text=True, capture_output=False)[source]#

创建并运行一个子进程。

是对 subprocess.run 的轻量级封装,用于测试。它会将 Cygwin 上的 fork() 失败标记为预期失败:这不是成功,但也不表示代码有问题。

参数:
args字符串列表
env字典[字符串, 字符串]
timeout浮点数
标准输出, 标准错误
check布尔值
text布尔值

在 subprocess 中也称为 universal_newlines。我选择此名称是因为其主要作用是返回字节 (False) 或字符串 (True),尽管它也尝试跨平台规范化换行符。

capture_output布尔值

将标准输出和标准错误设置为 subprocess.PIPE

返回:
procsubprocess.Popen
引发:
pytest.xfail

如果平台是 Cygwin 并且子进程报告 fork() 失败。

另请参阅

subprocess.run
matplotlib.testing.subprocess_run_helper(func, *args, timeout, extra_env=None)[source]#

在一个子进程中运行一个函数。

参数:
func函数

要运行的函数。它必须位于可导入的模块中。

*args字符串

要作为 subprocess.run 的第一个参数传递的任何额外命令行参数。

extra_env字典[字符串, 字符串]

为子进程设置的任何额外环境变量。

matplotlib.testing.compare#

用于比较图像结果的实用程序。

matplotlib.testing.compare.calculate_rms(expected_image, actual_image)[source]#

计算每像素误差,然后计算均方根误差。

matplotlib.testing.compare.comparable_formats()[source]#

返回此系统上 compare_images 可以比较的文件格式列表。

返回:
str 列表

例如 ['png', 'pdf', 'svg', 'eps']

matplotlib.testing.compare.compare_images(expected, actual, tol, in_decorator=False)[source]#

比较两个“图像”文件,检查公差范围内的差异。

给定的两个文件名可能指向可通过 converter 字典转换为 PNG 的文件。底层的均方根误差(RMS)使用 calculate_rms 函数计算。

参数:
expected字符串

预期图像的文件名。

actual字符串

实际图像的文件名。

tol浮点数

公差(颜色值差异,其中 255 是最大差异)。如果平均像素差异大于此值,则测试失败。

in_decorator布尔值

确定输出格式。如果从 image_comparison 装饰器调用,此值应为 True。(默认值=False)

返回:
无或字典或字符串

如果图像在给定公差内相等,则返回 None

如果图像不同,则返回值取决于 in_decorator。如果 in_decorator 为 True,则返回一个包含以下条目的字典:

  • rms:图像差异的均方根误差。

  • expected:预期图像的文件名。

  • actual:实际图像的文件名。

  • diff_image:差异图像的文件名。

  • tol:比较公差。

否则,将返回此信息的可读多行字符串表示。

示例

img1 = "./baseline/plot.png"
img2 = "./output/plot.png"
compare_images(img1, img2, 0.001)

matplotlib.testing.decorators#

matplotlib.testing.decorators.check_figures_equal(*, extensions=('png', 'pdf', 'svg'), tol=0)[source]#

用于生成和比较两个图形的测试用例的装饰器。

被装饰的函数必须接受两个关键字参数 fig_testfig_ref,并在其上绘制测试和参考图像。函数返回后,图形将被保存和比较。

在可能的情况下,应优先使用此装饰器而不是 image_comparison,以防止测试套件的规模膨胀。

参数:
extensions列表,默认值:["png", "pdf", "svg"]

要测试的扩展名。

tol浮点数

均方根误差(RMS)阈值,超过此阈值测试将被视为失败。

引发:
运行时错误

如果在测试函数内部创建了任何新图形(且未随后关闭)。

示例

检查使用单个参数调用 Axes.plot 是否会将其与 [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])
matplotlib.testing.decorators.image_comparison(baseline_images, extensions=None, tol=0, freetype_version=None, remove_text=False, savefig_kwarg=None, style=('classic', '_classic_test_patch'))[source]#

将测试生成的图像与 baseline_images 中指定的图像进行比较,两者必须一致,否则将引发 ImageComparisonFailure 异常。

参数:
baseline_images列表或 None

一个字符串列表,指定通过调用 Figure.savefig 生成的图像名称。

如果为 None,则测试函数必须使用 baseline_images fixture,可以作为参数或与 pytest.mark.usefixtures 一起使用。此值仅在使用 pytest 时允许。

extensionsNone 或 字符串列表

要测试的扩展名列表,例如 ['png', 'pdf']

如果为 None,则默认为所有支持的扩展名:png、pdf 和 svg。

当测试单个扩展名时,可以直接将其包含在传递给 baseline_images 的名称中。在这种情况下,不得设置 extensions

为了防止测试套件的规模膨胀,我们只在测试明确地使用了依赖于特定后端的功能时(另请参阅 check_figures_equal 装饰器),才包含 svgpdf 输出。

tol浮点数,默认值:0

均方根误差(RMS)阈值,超过此阈值测试将被视为失败。

由于浮点计算中预期的小差异,在 32 位系统上,此阈值会额外增加 0.06。

freetype_version字符串或元组

此测试通过所需的 FreeType 预期版本或版本范围。

remove_text布尔值

在比较之前从图形中移除标题和刻度文本。这有助于使基线图像独立于不同 FreeType 版本之间文本渲染的变化。

这不会移除其他更刻意的文本,例如图例和注解。

savefig_kwarg字典

传递给 savefig 方法的可选参数。

style字符串、字典或列表

应用于图像测试的可选样式。如果需要,测试本身也可以应用其他样式。默认值为 ["classic", "_classic_test_patch"]

matplotlib.testing.decorators.remove_ticks_and_titles(figure)[source]#

matplotlib.testing.exceptions#

exception matplotlib.testing.exceptions.ImageComparisonFailure[source]#

基类:AssertionError

引发此异常以将测试标记为两个图像之间的比较。

使用可选依赖项进行测试#

有关 fixtures 的更多信息,请参阅 pytest fixtures

matplotlib.testing.conftest.pd()#

用于导入和配置 pandas 的 fixture。使用此 fixture 时,如果未安装 pandas,则跳过测试。在测试文件中请使用此 fixture 而不是直接导入 pandas。

示例

通过将 pd 作为参数传递给测试来请求 pandas fixture

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)
matplotlib.testing.conftest.xr()#

用于导入 xarray 的 fixture,以便在未安装 xarray 时跳过测试。在测试文件中请使用此 fixture 而不是直接导入 xarray。

示例

通过将 xr 作为参数传递给测试来请求 xarray fixture

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)