armando armando - 6 months ago 17
Python Question

how to plot a structured data file with Python?

I have a data file which has the following structure:

cat data.dat
% 1
0.9 0.9 0.6 0.5
0.0 0.1 0.3 0.2
0.5 0.2 0.0 0.1
% 2
0.2 0.6 0.2 0.3
0.2 0.1 0.1 0.2
0.4 0.2 0.1 0.1


I just showed a simple example with only 2 sets of data but the file
is much more larger than this. What I want is to read the file with
Python and then tell Python to workout the set which starts with
% 2

as follows. From this set I would like to plot the row=1 w.r.t. the
array of integers 1,2,3,4 in this case.

I can do it with GNUPLOT but I spent lot of time because first I need
to cut the set I want and paste it in a temporary file. Second, I use AWK
to transpose the matrix.

I wonder if what I proposed is possible in Python.

Thanks.

Answer

Well, if you need a possibility to access various data from file (considering @gboffi post, there are a restriction that set labels are in consecutive order):

from matplotlib import pyplot as plt

data = []

with open('cat_data.dat') as f:
    for line in f:
        if line[1].isalpha():
            continue
        if line.startswith('%'):
            data.append([])
            continue
        data[-1].append([float(x) for x in line.split()])

# Now you have all values in data:
# [[[0.9, 0.9, 0.6, 0.5], [0.0, 0.1, 0.3, 0.2], [0.5, 0.2, 0.0, 0.1]], [[0.2, 0.6, 0.2, 0.3], [0.2, 0.1, 0.1, 0.2], [0.4, 0.2, 0.1, 0.1]]]

_set = 2
row = 1 # Corresponds to 0.2  0.1  0.1  0.2

plt.plot(range(1, 5), data[_set - 1][row])
plt.show()

enter image description here