title: Matplotlib saving state between different uses of io.bytesIo
body: <p>I have a function that returns the raw contents of a .png:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  def plotVoltage(dbc,imei,sMonth,sDay,sYear):
      ...
      plt.figure(1) 
      ax = plt.subplot(111)
      ax.plot(Xs,Ys,color='blue',linewidth=2)
  
      buf = None #DOES NOT SOLVE PROBLEM
      buf = io.BytesIO()
      plt.savefig(buf, format = 'png')
      buf.seek(0)
      return buf.read() 
  </code></pre>
  
  <p>And a flask function which returns the image as an HTTP response:</p>
  
  <pre><code>@app.route('/voltageOverTime', methods=['GET'])
  def vot():
     ret = plotVoltage(some_parameters)) 
     resp = Response(response=ret,
                  status=200,
                  mimetype="image/png")
     return resp
  </code></pre>
  
  <p>Then finally somwhere I have a .html file where the image source is set to <code>/voltageOverTime</code> with some parameters when a button is clicked. The problem is, if I click the button multiple times with different parameters, the images "pile on top" of each other; I see each line plotted on top of one another. I think this is related to the memory buffer, e.g., each time I click the button it just adds to this buffer. How can I make this buffer completely flush?</p>