0

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.

eshirvana
  • 23,227
  • 3
  • 22
  • 38
user16454053
  • 101
  • 1
  • 9
  • I assume from your question, you want to save the plots not the data. The answer will depend on the format in which you want to save the plot image. See [Save plot to image file instead of displaying it using Matplotlib](https://stackoverflow.com/questions/9622163/save-plot-to-image-file-instead-of-displaying-it-using-matplotlib) for an approach – itprorh66 May 13 '22 at 13:16
  • Yes you are right I want to save the plot. However I know how to save the plot, but I do not know how to save multiple plots coming out from loop with different names. The .png is ok. – user16454053 May 13 '22 at 13:25

1 Answers1

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

itprorh66
  • 3,110
  • 4
  • 9
  • 21
  • Excellent, thanks soo much it works :) Where I can find explanation what this line exactly doing .... just to better understand what is going on? – user16454053 May 13 '22 at 13:33
  • 1
    See [Python 3's f-Strings: An Improved String Formatting Syntax (Guide)](https://realpython.com/python-f-strings/) for a good tutorial on F string formatting – itprorh66 May 13 '22 at 13:38