Memory overflow when saving Matplotlib plots in a loop

后端 未结 2 624
礼貌的吻别
礼貌的吻别 2021-01-13 12:39

I am using an iterative loop to plot soame data using Matplotlib. When the code has saved around 768 plots, it throws the following exception.

RuntimeError:          


        
2条回答
  •  野的像风
    2021-01-13 13:22

    As a slightly different answer, remember that you can re-use figures. Something like:

    fig = plt.figure()
    ax = plt.gca()
    
    im = ax.imshow(data_list[0],...)
    
    for new_data in data_list:
        im.set_cdata(new_data)
        fig.savefig(..)
    

    Which will make your code run much faster as it will not need to set up and tear down the figure 700+ times.

提交回复
热议问题