0

I am trying to save multiple plots I got after I run my code but when I save my fig it only save the last one, I have seen that the solution it is to save in a loop so it saves each figure individually but my lack of experience in python just dont let me to figure out how to do it?

Any help??

#eyfp is a dataframe I obtained from my main dataframe
eyfp= df[df.GROUP=="eYFP"]
for title, group in eyfp.groupby('ID'):
    fig= group.plot(x='DAY', y=['INACTIVE','ACTIVE'], title= title)
    plt.ylim(0,1500)
    plt.xlim(1,10,1)
    plt.xlabel('DAY', fontsize= 17)
    fig.set_yticklabels(["", "200", "400", "600", '800','1000','1200','1400'], fontsize=15)
    fig.set_xticklabels(["1", "2", "3", "4", '5','6','7','8','9','10'], fontsize=15)
    plt.ylabel('# wheel turns', fontsize= 17)
  • I would use [`plt.savefig(...)`](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html) followed by `plt.close()`. For your figure name, you'd need some sort of string. Perhaps an [f-string](https://datagy.io/python-f-strings/) like `f'{title}.png'` so your command would be `plt.savefig(f'{title}.png')`. All of this would be inside your loop. – ramzeek Feb 06 '22 at 21:00
  • That worked! Thank you – Osnaya Ivn Feb 06 '22 at 21:51

0 Answers0