Python Forum

Full Version: How to extract only the last result from a code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I have a code that plot an histogram for each iteration of a processus
but i want that export only the last histogram of the last iteration

please what can i modify in this code ?

Thank you very much


import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pylab

sns.set()

importdata=open('Hmz_OT_MULC_Includes.dens','r+')
data=importdata.readlines()
importdata.close()



tri=[]
j=0
for i in range(len(data)):
    if 'iter' in data[i]:
        l=i
        nbre_iter=[]        
        while l+1 < len(data) and 'iter' not in data [l+1]:    
            l+=1
            nbre_iter.append(data[l])  

        tri.append(nbre_iter)
        j+=1


tri_final=[]    
for i in range(len(tri)):
    extract_density=[]   
    print('i=',i)
    for j in range(len(tri[0])):
        extract_density.append(tri[i][j][12 :])
    tri_final.append(extract_density) 
       

convert_into_float=[]    
for i in range(len(tri_final)):
    convert=[]   
    for j in range(len(tri_final[0])):
        convert.append(float(tri_final[i][j]))
    convert_into_float.append(convert)        
 
for i in range(len(convert_into_float)):
    nbre_elements=np.zeros(10)
    for j in range(len(convert_into_float[0])):       
        if 0<= convert_into_float[i][j]< 0.1:
            nbre_elements[0]+=1
        elif 0.1<= convert_into_float[i][j] <0.2:
            nbre_elements[1]+=1
        elif 0.2<= convert_into_float[i][j] <0.3:
            nbre_elements[2]+=1
        elif 0.3<= convert_into_float[i][j] <0.4:
            nbre_elements[3]+=1                         
        elif 0.4<= convert_into_float[i][j] <0.5:
            nbre_elements[4]+=1                               
        elif 0.5<= convert_into_float[i][j] <0.6:
            nbre_elements[5]+=1                
        elif 0.6<= convert_into_float[i][j] <0.7:
            nbre_elements[6]+=1       
        elif 0.7<= convert_into_float[i][j] <0.8:
            nbre_elements[7]+=1
        elif 0.8<= convert_into_float[i][j] <0.9:
            nbre_elements[8]+=1
        elif 0.9<= convert_into_float[i][j] <=1:
            nbre_elements[9]+=1
                         
                         
    fig = plt.figure()
    x=[1,2,3,4,5,6,7,8,9,10]
    width = 1.3
    BarName = ['0.0-0.1','0.1-0.2','0.2-0.3','0.3-0.4','0.4-0.5','0.5-0.6','0.6-0.7','0.7-0.8','0.8-0.9','0.9-1']
    plt.bar(x, nbre_elements, width, color='b' )
    nom_figure='Histogramme_ITER_N_'+str(i)+ '.png'
    plt.grid()
    plt.ylabel('Nombre d elements')
    plt.title('Nombre d elements en fonction de leur densite_ITER_N_'+str(i))

    pylab.xticks(x, BarName, rotation=40)

    plt.savefig(nom_figure)
                                         
What is the content of convert_into_float?

for j in range(len(convert_into_float[0])):
If it is the data that you want to extract only the last content, you can try this:
#for i in range(len(convert_into_float)):
nbre_elements=np.zeros(10)
for j in range(len(convert_into_float[-1])):
    if 0<= convert_into_float[-1][j]< 0.1:
    ...