Search code examples
pythonmatplotlibin-memory

Keeping matplotlib images in-memory until it is ready to be plotted


I have a situation where in a loop, I perform some calculations, then plot the results and assign a score to each of the plots. At the end, I sort the plots by their scores in descending order. Maybe the top quarter is needed and the rest discarded. I then generate a html file with the saved images.

The plotting and saving slows down the process. Is there some way of storing matplotlib images in memory?

For the MCVE, consider the following code.

from matplotlib import pyplot as plt
import random

score_list = []
for a in range(50):
    score = random.random()
    fig = plt.figure()
    ax = plt.subplot2grid((1, 1), (0, 0))
    ax.plot([0, 0], [1, 1], 'r--')
    fig.savefig('{}.png'.format(a))
    plt.close(fig)
    score_list.append(['{}.png'.format(a), score])

sorted_score_list = sorted(score_list, key=lambda x: x[1], reverse=True)
print(sorted_score_list)

The MCVE might have over-simplified the issue. The scoring actually dictates what gets plotted. Carrying that information around and delaying the plotting is not undoable but I am wondering if there's some other solution where I can keep a certain number of images in-memory.


Solution

  • You could probably achieve what you want by affecting an specific index to your figure and then getting them later using the same index :

    from matplotlib import pyplot as plt
    import random
    
    score_list = []
    for a in range(50):
        score = random.random()
        fig = plt.figure(a)
        ax = plt.subplot2grid((1, 1), (0, 0))
        ax.plot([0, 0], [1, 1], 'r--')
        score_list.append(['{}'.format(a), score])
    
    sorted_score_list = sorted(score_list, key=lambda x: x[1], reverse=True)
    
    # Saving the first 10 ones by score
    for idx, score in sorted_score_list[:10]:
        fig = plt.figure(idx)
        fig.savefig('{}.png'.format(idx))
        plt.close(fig)