Candice_Burke
Candice_Burke

Reputation: 13

Saving Multiple Plots with Different Names

I'm trying to save multiple plots to a directory. Problem is, I don't want to use a counter for the different file names because they all have different ID numbers, denoted as trk_id, or j. If you need more of the code, please let me know! Plus, I know this code just uses the same name and overwrites each file.

  for i, j in enumerate(trk_id):
          t = np.arange(0, 3*3600) + t0_b[i]
          g_x = f_r(tau_b[i], t0_b[i], c0_b[i], c1_b[i], c2_b[i])
          fig,ax = plt.subplots()
          ax.plot(t, g_x(t))
          plt.yscale('log')
          plt.ylabel('Height (arcsec)')
          plt.xlabel('Time (s)')
          ax.set_title(j)
          plt.savefig('plots/j.png')

Upvotes: 1

Views: 2488

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339230

In order to use the loop variable j as filename, you can generate a string like

filename = 'plots/' + str(j) +'.png'
plt.savefig(filename)

or

filename = 'plots/{}.png'.format(j)
plt.savefig(filename)

Upvotes: 1

Related Questions