0

My data is look like as in the picture. All of my datas are in .txt format and my aim is to loop over files and plot them. First row represents my variables (WL, ABS, T%) so firstly I need to delete them before proceeding.

with open('Desktop/100-3.txt', 'r') as f:
data = f.read().splitlines(True)
with open('Desktop/100-3.txt', 'w') as f:
f.writelines(data[1:])

Probably it would not be necessary but I am very new in Numpy. Basically the algorithm will be as follows:

  1. Read all the .txt files
  2. Plot T% versus WL, plot ABS versus WL, save. (WL -> x variable)
  3. Continue for the next file, .. (two graphs for every .txt file)
  4. Then finish the loop, exit.

data looks like this

What I've tried

from numpy import loadtxt
import os
dizin = os.listdir(os.getcwd())
for i in dizin:
  if i.endswith('.txt'):
   data = loadtxt("??",float)

1 Answer 1

2

For data files like this I would prefer np.genfromtxt over np.loadtxt, it has many useful options you can look up in the docs. The glob module is also nice to iterate over directories with wildcards as filters:

from glob import glob
import numpy as np
import matplotlib.pyplot as plt

# loop over all files in the current directory ending with .txt
for fname in glob("./*.txt"):
    # read file, skip header (1 line) and unpack into 3 variables
    WL, ABS, T = np.genfromtxt(fname, skip_header=1, unpack=True)

    # first plot
    plt.plot(WL, T)
    plt.xlabel('WL')
    plt.ylabel('T%')
    plt.show()
    plt.clf()

    # second plot
    plt.plot(ABS, T)
    plt.xlabel('WL')
    plt.ylabel('ABS')
    plt.show()
    plt.clf()

The next step would be to do some research on matplotlib to make the plots look better.

Please let me know if the code does not work, I'll try to fix it then.

EDIT: Added plt.clf() to clear the figure before creating a new one.

Sign up to request clarification or add additional context in comments.

11 Comments

The result is in here: postimg.org/image/r6u0kajib Even though this code is very good that is not quite what I'm looking for. I want to save two figure for every text file separately. Can we improve it for that?
Some helpful information has given in here: stackoverflow.com/questions/9622163/
I forgot to reset the plot after each iteration, so I added plt.clf() after each plt.show().
StopIteration error occured (Just copied the code you have shared) 1466 # Skip the first skip_header rows 1467 for i in range(skip_header): -> 1468 next(fhd) 1469 1470 # Keep on until we find the first valid values
I don't exactly know but that sounds like it tried to read an empty .txt file. Are you sure you only have .txt files with data in it in the directory where you execute your script?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.