渲染MathText以获取其边界框时Matplotlib内存泄漏

时间:2018-12-18 19:36:46

标签: matplotlib memory tex

在循环中调用get_window_extent()以使包含MathText的Text对象增加了使用的内存,尽管已删除了Text对象。最小的工作示例:

import matplotlib.pyplot as plt
import random


fig = plt.figure()
renderer = fig.canvas.get_renderer()
sample_text = ' '.join("${}$".format(random.random())
                       for _ in range(1000))
while True:
    # This wouldn't make memory usage grow:
    # text = sample_text

    # This wouldn't either:
    # text = ' '.join("{}".format(random.random())
    #                 for _ in range(1000))

    # But this does:
    text = ' '.join("${}$".format(random.random())
                    for _ in range(1000))
    # ...when combined with get_window_extent():
    plotted_text = fig.text(0, 0, text)
    plotted_text.get_window_extent(renderer)
    plotted_text.remove()

任何想法为何以及如何预防?

0 个答案:

没有答案