Why `plt.savefig` is so slow ? Could I somehow speed it up?

1.1k views Asked by At

Creation of 100 charts in less then second, but writing them on disk on converting to base64 representation without writing them on disk takes 10 second, so the overhead is in creation of png/jpg representation itself, is there way to speed plt.savefig ?

With bbox_inches="tight" there is futher slowdown, so I guess there could be some way to speed things up.

All jumps in execution time are between steps 2 and 3 (logging with datetime.datetime.now();print is enought is this case I think):

import numpy as np
import datetime
data = np.random.rand(20, 100)

list_base64 = []
path = 'YOUR_PATH'
file = "test.jpg"

for i in range(data.shape[1]):
    ct = datetime.datetime.now();print(f"current time: {i} 1", ct)
    if i==0:
        ct0=ct
    plt.plot(data[:,i])
    ct = datetime.datetime.now();print(f"current time: {i} 2", ct)
    
    plt.savefig(path + file, dpi=10, bbox_inches="tight")
    ct = datetime.datetime.now();print(f"current time: {i} 3", ct)

    plt.close()
    ct = datetime.datetime.now();print(f"current time: {i} 4", ct)
    
    plot_file = open(path + file, 'rb')
    ct = datetime.datetime.now();print(f"current time: {i} 5", ct)

    base64_string = base64.b64encode(plot_file.read()).decode()
    ct = datetime.datetime.now();print(f"current time: {i} 6", ct)

    plot_file.close()
    ct = datetime.datetime.now();print(f"current time: {i} 7", ct)

    base64_string = chart_to_base64(plt)
    ct = datetime.datetime.now();print(f"current time: {i} 8", ct)

    list_base64.append(base64_string) 
    ct = datetime.datetime.now();print(f"current time: {i} 9", ct)
print('loop starts at:', ct0)
0

There are 0 answers