'Creating multiple figures out of for loop
I am trying to loop through my table and to create 3 different figures. This is my code ....
tab_stat = pd.read_table('test.txt', delim_whitespace=True)
radius_single = tab_stat.Re_in_pc.unique()
for i in radius_single:
single_stat = tab_stat.loc[tab_stat['Re_in_pc'] == i]
Re_16_84_perc = [single_stat['Re_gal_p16'],single_stat['Re_gal_p84']]
Re_std = [single_stat['Re_gal_std'],single_stat['Re_gal_std']]
ax = plt.subplot()
ax.tick_params(axis='both', which='both', direction="in")
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.scatter(single_stat['mag_in_obs'], single_stat['Re_gal_med_pc'] , marker='D', color='orange', edgecolors='k', linewidth=1, s=500, alpha=0.6)
plt.errorbar(single_stat['mag_in_obs'], single_stat['Re_gal_med_pc'], yerr=Re_16_84_perc, ls = "None", elinewidth=0.5, ecolor = 'k', capsize=3, capthick=1 )
plt.show()
This code creates three different figures, I would like to save them with different names but no idea how. Trying to give them name based on i
variable located in for loop.
I tried this:
plt.savefig('%%_name' %i)
But does not work ..... any sugestions.
Data I am using are here:
Re_in_pc mag_in_obs Re_gal_med_pc Re_gal_p16 Re_gal_p84 Re_gal_mean Re_gal_std
5.97 28.99 20.28 11.57 33.28 22.2 11.0
5.97 26.83 12.27 9.52 16.56 12.49 3.27
10.03 28.99 20.53 9.64 36.68 24.16 14.4
10.03 26.83 14.96 11.58 18.84 15.09 3.65
20.0 28.99 25.42 12.62 44.85 29.13 15.21
20.0 26.83 26.42 21.25 29.34 25.4 4.61
Thanks.
Solution 1:[1]
I believe the solution is to include within your loop the following command:
plt.savefig(f'foo_{i}.png')
where i is the index number of the loop
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | itprorh66 |