title: Saving multiple figures from each file for multiple files separately
body: <p>I have a code that can read and run multiple files and generates multiple figures from each file. I want to save all the figures separately. But I can save only one figure from each file. For example, I have 17 files, each file generates 3 figures, so I should have 51 figures in total. The code displays all the 51 figures(3 from each file) but saves only one figure from each file, 17 figures in total. How can I save all the figures separately?</p>
  
  <pre class="lang-py prettyprint-override"><code>    import os
      import numpy as np
      import pandas as pd
      import matplotlib.pyplot as plt
  
      for file in os.listdir(r'/mydir'):
          if file.endswith(".txt"):
              print(os.path.join("/mydir", file))
              life_time= pd.read_csv(file, sep = "\t")
              life_time.columns = ["Time" , "Counts"]
              time= life_time.Time[2:599]
              time_1=time-1e-06
              x1=time_1.reset_index(drop=True)
              bin1 = life_time.Counts[2:599]
              bin2 = life_time.Counts[602:1199]
              bin3 = life_time.Counts[1202:1799]
              """Reser the index"""
              r_bin1=bin1.reset_index(drop=True)
              r_bin2=bin2.reset_index(drop=True)
              r_bin3=bin3.reset_index(drop=True)
  
  
              x= x1[1:598]
              y1=r_bin1[1:598]
              y2=r_bin2[1:598]
              y3=r_bin3[1:598]
      #From each dataset I have created three y value such as y1, y2, y3 and then created a list
              y = [y1, y2, y3]
              def exponenial_func(x, a, b, c):
                  return a*np.exp(-b*x)+c
              for i in y:
                  popt, pcov = curve_fit(exponenial_func, x, i)
                  a, b, c = popt
                  yy = exponenial_func(x, *popt)
                  plt.plot(x,i)
                  plt.plot(x, yy, 'r--')
                  plt.savefig("file{}.jpg".format(file), dpi = 600, bbox_inches = "tight")
                  plt.show()
  </code></pre>
  
  <p>For example, my file names are USA_1, USA_2......etc. I want to save the figure  with the figure name USA_1_bin1, USA_1_bin2, USA_1_bin3, USA_2_bin1, USA_2_bin2, USA_2_bin3....      </p>