Looping over files and plotting (Python)
<p>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. </p>

<pre><code>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:])
</code></pre>

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

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

<p><a href="http://i.stack.imgur.com/GOvbi.jpg" rel="nofollow">data  looks like this</a></p>

<p><strong>What I've tried</strong></p>

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

<p>For data files like this I would prefer <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html" rel="nofollow">np.genfromtxt</a> over np.loadtxt, it has many useful options you can look up in the docs. The <a href="https://docs.python.org/3/library/glob.html" rel="nofollow">glob</a> module is also nice to iterate over directories with wildcards as filters:</p>

<pre><code>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()
</code></pre>

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

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

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

python numpy plot