首页 > 解决方案 > Jupyter 在打印之前不显示多处理的 matplotlib 图

问题描述

我正在处理一个大数据集,我想并行绘制它以加快速度。我可以轻松地创建绘图并将它们保存到磁盘,但是当我在每个进程中强制打印时,它们只会出现在我的 JupyterLab 笔记本中。有什么办法可以避免打印吗?

%matplotlib inline 

def task(args):
    i, path = args       
    print("WHY PYTHON WHY???")  # <-- plot will not be visible without this
    matplotlib.font_manager._get_font.cache_clear()  # necessary to reduce corruption artifacts
    values = range(10000)
    plt.figure(figsize=(40,10))
    plt.violinplot(values)
    plt.show()
    plt.savefig(path)
    return i

paths = [f"figs/{i}.png" for i in range(9)]

with Pool(10) as pool:
    for i in pool.imap(task, zip(range(9), paths)):
        pass

标签: pythonpython-3.xmatplotlibjupyter-notebookpython-multiprocessing

解决方案


推荐阅读