Python 如何关闭测试图形matplotlib
我在一个模块中进行了一系列图像比较测试。当使用pytest时,我得到一个内存不足的警告,我假设这是由于打开了多个测试图像20+。我该如何清洗或关闭它们?由于我的代码设置,fig.close无法工作。我在下面附上了一个测试示例Python 如何关闭测试图形matplotlib,python,testing,matplotlib,Python,Testing,Matplotlib,我在一个模块中进行了一系列图像比较测试。当使用pytest时,我得到一个内存不足的警告,我假设这是由于打开了多个测试图像20+。我该如何清洗或关闭它们?由于我的代码设置,fig.close无法工作。我在下面附上了一个测试示例 @pytest.mark.mpl_image_compare(baseline_dir='baseline', filename='file.png', style=
@pytest.mark.mpl_image_compare(baseline_dir='baseline',
filename='file.png',
style=('style_guidline'),
savefig_kwargs={'bbox_inches': 'tight'},
tolerance=5)
def test_3(self):
data = pd.read_csv('test.csv')
fig = module.create_figure(
data=data,
kind="bar_chart",
)
return fig
一个万无一失的解决方案是关闭所有打开的数字:
>>> plt.close('all')
例如:
>>> import matplotlib.pyplot as plt
...
>>> fig1, ax1 = plt.subplots()
... fig2, ax2 = plt.subplots() # 2 separate figures
...
>>> plt.get_fignums()
...
[1, 2]
>>> plt.close('all')
>>> plt.get_fignums()
...
[]
此处的其他选项: