简体   繁体   中英

np.loadtxt not overwritting files

I am importing a list that is not being overwritting by np.loadtxt. I have 6 text files that are being added into the same array, but what I would like to do is overwrite this array and print a new graph per file. Unfortunately it's printing a new image with one graph ontop of the other per file until it reaches the length( looking like graph1.jpg, graph1 +2 .jpg, graph1 + 2 + 3.jpg and so on). Can anyone help me with this? I found the problem to be in the line containing "np.loadtxt....". But I do not know what other code to use to pull up a text file and pull a column from it while overwriting the previous loop's code. Below is a portion of my code. Please not that all_txt_PL is a list of file txt names.

while i!= x2:
    DataIn = np.loadtxt(all_txt_PL[i])
    #DataIn = np.array(DataIn)
    #print(DataIn, '\n')
    y = list()
    v = list()
    for column in DataIn:
        v.append(column[1])
        y.append(column[0])
    plt.plot(y,v, 'b')  
    plt.title('PL Spectrum')
    plt.ylabel('Intensity A.U')
    plt.xlabel('energy [eV]')
    DataIn_PL_name = str(all_txt_PL[i])
    #graphs_PL.append('PL_plot_coordinates_' + DataIn_PL_name.strip('.txt') + '.JPG')
    #plt.savefig('PL of ' + DataIn_PL_name.strip('.txt') + '.JPG')
    #print(i, all_txt_PL[i], '\n')
    i += 1
    #DataIn = np.delete(DataIn, [0,1], axis = None)
    #DataIn = np.delete(DataIn, 0, None)
    y[:] = []
    v[:] = []

I think the issue is with your plotting code. If I understand you correctly, you want each plot to be its own figure. Unless you make explicitly tell matplotlib otherwise, it will continue to add all your plots to the same axes.

Try adding a call to plt.figure() at the end of your loop.

Or, you can look up matplotlib subplots if you want them to be in a grid with one another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM