Python - Graphing contents of mutliple files
<p>I have lists of ~10 corresponding input files containing columns of tab separated data approx 300 lines/datapoints each. </p>

<p>I'm looking to plot the contents of each set of data such that I have a 2 plots for each set of data one is simply of x vs (y1,y2,y3,...) and one which is transformed by a function e.g. x vs (f(y1), f(y2),f(y3),...).</p>

<p>I am not sure of the best way to achieve it, I thought about using a simple array of filenames then couldn't work out how to store them all without overwriting the data - something like this: </p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt 

def ReadDataFile(file):
    print (file)
    x,y  = np.loadtxt(file, unpack=True, usecols=(8,9))
    return x, y

inputFiles = ['data1.txt','data2.txt','data2.txt',...]
for file in inputFiles:
    x1,y1 = ReadDataFile(file) ## ? ##
    p1,q1 = function(x1,y1)    ## ? ##

plt.figure(1)
plt.plot(x1,y1)
plt.plot(x2,y2)
...
# plt.savefig(...)

plt.figure(2)
plt.plot(p1,q1)
plt.plot(p2,q2)
...
# plt.savefig(...)
plt.show()
</code></pre>

<p>I guess my question is how to best read and store all the data and maintain tha ability to access it without needing to put all the code in the readloop.  Can I read two data sets into a list of pairs? Is that a thing in Python? if so, how do I access them?</p>

<p>Thanks in advance for any help! 
Best regards! </p>

<p>Basically, I think you should put all your code in the readloop, because that will work easily. There's a slightly different way of using matplotlib that makes it easy to use the existing organization of your data AND write shorter code. Here's a toy, but complete, example: </p>

<pre><code>import matplotlib.pyplot as plt
from numpy.random import random

fig, axs = plt.subplots(2)

for c in 'abc':                    # In your case, for filename in [file-list]:
    x, y = random((2, 5))
    axs[0].plot(x, y, label=c)     # filename instead of c in your case
    axs[1].plot(x, y**2, label=c)  # Plot p(x,y), q(x,y) in your case

axs[0].legend()   # handy to get this from the data list

fig.savefig('two_plots.png')
</code></pre>

<p><a href="http://i.stack.imgur.com/8eCxU.png" rel="nofollow"><img src="http://i.stack.imgur.com/8eCxU.png" alt="enter image description here"></a></p>

<p>You can also create two figures and plot into each of them explicitly, if you need them in different files for page layout, etc:</p>

<pre><code>import matplotlib.pyplot as plt
from numpy.random import random

fig1, ax1 = plt.subplots(1)
fig2, ax2 = plt.subplots(1)

for c in 'abc':                    # or, for filename in [file-list]:
    x, y = random((2, 5))
    ax1.plot(x, y, label=c)
    ax2.plot(x, y**2, label=c)

ax1.legend()   
ax2.legend()

fig1.savefig('two_plots_1.png')
fig2.savefig('two_plots_2.png')
</code></pre>

python matplotlib