title: How to store a png from Matplotlib figure in a variable?
body: <p>I have a code that creates 20 images that I want to save as a GIF. Instead of saving each image individually and then reloading them into the program to make a GIF out of them, I want the png versions of the axis to be stored in a list, so that I can skip the saving and reloading part.</p>
  <p>Here is the way I currently do it:</p>
  <pre><code>from matplotlib import pyplot as plt
  import imageio
  
  def make_gif(gif_direc, video_frames):
      fig1 = plt.Figure()
      axes = fig1.add_subplot()
  
      for j in range(20):
          axes.clear()
          # Here I would plot all the stuff onto the axis.
          fig1.savefig(gif_direc + &quot;Frame_&quot; + str(j + 1) + &quot;.png&quot;, format=&quot;png&quot;)
  
      images = []
      for j in range(20):
          images.append(imageio.imread(gif_direc + &quot;Frame_&quot; + str(j + 1) + &quot;.png&quot;))
  
      # Now the GIF is saved.
      imageio.mimsave(gif_direc + &quot;Particle Impact.gif&quot;, images, duration=0.25)
  </code></pre>