用不同的名称保存多个图 [英] Saving Multiple Plots with Different Names

查看:49
本文介绍了用不同的名称保存多个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将多个图保存到一个目录中.问题是,我不想对不同的文件名使用计数器,因为它们都有不同的 ID 号,表示为 trk_id 或 j.如果您需要更多代码,请告诉我!另外,我知道这段代码只是使用相同的名称并覆盖每个文件.

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')

推荐答案

为了使用循环变量 j 作为文件名,可以生成一个类似

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

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

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

这篇关于用不同的名称保存多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆