I am trying to write a python Code snippet which creates 24 figures and 9 subplot and saves them in the directory. Its taking around 197 sec to create all the 24 figures and save them. This is implemented Raspberry Pi 3. How could I optimize the code further to obtain a better Speed up?
import numpy
import matplotlib
matplotlib.use('Agg') #changed the backend
import time
import matplotlib.pyplot as plt
start_program = time.time()
for i in range(24):
start = time.time()
fig = plt.figure(num = i, figsize = (20, 10))
string = 'Title of the graph ' + str(i)
fig.suptitle(string, fontsize =20)
for j in range(9):
ax = fig.add_subplot(3,3,j+1)
ax.cla()
string = 'subgraph title' + str(j)
ax.title.set_text(string)
ax.axis([0,60,0,35])
ax.grid(color = 'g', linestyle = '-', linewidth = 1)
string = 'save_fig'
string = string + str(i) +'.png'
fig.savefig(string)
print("Total time taken to save fig ",i, "is", time.time()-start)
end_program = time.time()
print("Total time to complete program is :", end_program - start_program)
And the output Looks like this
Total time taken to save fig 0 is 7.201676845550537(Worried about 7sec time to create a figure and save it)
Total time taken to save fig 1 is 7.540156602859497
Total time taken to save fig 2 is 9.28951120376587
Total time taken to save fig 3 is 7.745286464691162
Total time taken to save fig 4 is 9.9215247631073
Total time taken to save fig 5 is 8.051039695739746
Total time taken to save fig 6 is 8.413954019546509
Total time taken to save fig 7 is 8.299449920654297
Total time taken to save fig 8 is 8.348041296005249
Total time taken to save fig 9 is 9.227581977844238
Total time taken to save fig 10 is 11.946017026901245
Total time taken to save fig 11 is 7.378983020782471
Total time taken to save fig 12 is 7.357311010360718
Total time taken to save fig 13 is 8.607544660568237
Total time taken to save fig 14 is 7.370815992355347
Total time taken to save fig 15 is 7.3785529136657715
Total time taken to save fig 16 is 8.844587087631226
Total time taken to save fig 17 is 7.358648777008057
Total time taken to save fig 18 is 7.3571202754974365
Total time taken to save fig 19 is 7.349349737167358
Total time taken to save fig 20 is 9.172396898269653
Total time taken to save fig 21 is 7.359432935714722
Total time taken to save fig 22 is 7.376481533050537
Total time taken to save fig 23 is 7.487479209899902
Total time to complete program is : 197.89335536956787
How can I reduce the total time?
