0

I am doing my final thesis and I need to create some gifs for a lot of different systems, the problem is that my code is REALLY slow and I need a way to increase the speed, I'm using imageio to do it

    fig,ax=plt.subplots()
plt.title("Variacion diaria")
plt.xlim(0,max(dia)+1)
plt.ylim(0,max(nstot)+1)
filenames=[]

plt.plot(nstot[:1],'b',label=("Susceptibles totales"))
plt.plot(nitot[:1],'r',label=("Infectados totales"))
plt.plot(nrtot[:1],'g',label=("Recuperados totales"))
plt.plot(ndtot[:1],'k',label=("Muertos totales"))
plt.legend()
for i in range(0,diax):
    plt.plot(nstot[:i],'b')
    plt.plot(nitot[:i],'r')
    plt.plot(nrtot[:i],'g')
    plt.plot(ndtot[:i],'k')
    #Create frame and saving it
    filename=f'{i}.png'
    filenames.append(filename)
    #save frame
    plt.savefig(filename)
    plt.close

#gif construction
with imageio.get_writer("gifprueba.gif" , mode='?') as writer:
    for filename in filenames:
        image=imageio.imread(filename)
        writer.append_data(image)
#Delete frames 
for filename in set(filenames):
    os.remove(filename)

Is there any way, library or method to make it faster?

Harald K
  • 26,314
  • 7
  • 65
  • 111
DaniDm
  • 3
  • 2
  • which part is slow? matplotlib plotting and saving the images? Or imageio constructing the gif from the images? – tmdavison Apr 15 '21 at 11:19
  • Consider using matplotlib animations instead of imageio. Check the example in this section: https://matplotlib.org/stable/api/animation_api.html#funcanimation – Pushkar Nimkar Apr 15 '21 at 11:21
  • The part where it has to plot every frame and saving it, after that the counstruction itself is relative fast. I was just wondering if there is a quicker way, because im using the same code for create a gif for representing a 300x300 system, each cell with one color, and it takes too much, because i need to create a lot of them – DaniDm Apr 15 '21 at 11:22
  • Here's a reference to export the animation as GIF. https://stackoverflow.com/questions/25140952/matplotlib-save-animation-in-gif-error – Pushkar Nimkar Apr 15 '21 at 11:24

0 Answers0