在python中使用n组x和y值分析和绘制数据文件(.dat)(Analysis and plotting from a data file (.dat) with n sets of x and y values in python)

我一直在尝试分析.dat文件中的数据。 文件中的实验重复(非常多次),使得每个实验对于每个r实验具有n个数据点。 例如:r = 4个实验,每个实验中n = 3个数据点:

1 4.8 2 3.4 3 2.3 1 6.5 2 5.3 3 4.2 1 9.8 2 8.4 3 7.6 1 13.8 2 12.4 3 11.6

我想阅读文件并绘制4个图表 - 前3个,第3个和第3个和第3个3行。 到目前为止我的代码是这样的:

for line in myfile: if not line.strip():#takes out empty rows continue else: data.append(line) for line in data: x, y = line.split() timestep.append(float(x)) value.append(float(y)) z = 0.0 j = 1 n = 3 #no. of data points in one experiment r = 4 #no. of times experiment repeats x = np.arange(1,n) for k in range(1, r): for i in (value): j += 1 if n%j != 0: #trying to break the loop after the first experiment of n data points z = i y_"str(j)" = [] #I want to call this array y_j, i.e. y_1 for the first loop or y_2 for the second, etc, wild index in python?! :( y_"str(j)".append(z) else: value = value[steps:] #trying to remove the first three points before starting to for loop again plt.figure() plt.plot(x, y_str(j),'r', label = "y_str(j)") plt.title('y ' +str(j) ) plt.show()

我将更多地分析它,但我很难在大数据数据中每n次执行相同的分析(绘图等)。 甚至可能不需要将我的2列输入数据拆分为单独的x和y列,但是我在for循环中使用data [i] [2]来获取恼人的int和float错误。

非常感谢您的帮助!

I've been trying to analyse data from a .dat file. The experiment in the file repeats (very many) times, such that each experiment has n data points for each of the r experiments. As an example: r = 4 experiments, with n = 3 data points in each experiment:

1 4.8 2 3.4 3 2.3 1 6.5 2 5.3 3 4.2 1 9.8 2 8.4 3 7.6 1 13.8 2 12.4 3 11.6

I want to read the file and plot 4 graphs - the first 3, second 3 and third 3 and fourth 3 rows. My code so far is this:

for line in myfile: if not line.strip():#takes out empty rows continue else: data.append(line) for line in data: x, y = line.split() timestep.append(float(x)) value.append(float(y)) z = 0.0 j = 1 n = 3 #no. of data points in one experiment r = 4 #no. of times experiment repeats x = np.arange(1,n) for k in range(1, r): for i in (value): j += 1 if n%j != 0: #trying to break the loop after the first experiment of n data points z = i y_"str(j)" = [] #I want to call this array y_j, i.e. y_1 for the first loop or y_2 for the second, etc, wild index in python?! :( y_"str(j)".append(z) else: value = value[steps:] #trying to remove the first three points before starting to for loop again plt.figure() plt.plot(x, y_str(j),'r', label = "y_str(j)") plt.title('y ' +str(j) ) plt.show()

I'll be analysing it more, but I'm just having difficulty in performing the same analysis (plotting, etc) every n times in the big array of data. It might not even be necessary to split my 2 column input data into separate x and y columns, but I was getting annoying int and float errors using data[i][2] in the for loop.

Thanks very much for any help!

最满意答案

通过实验读入数据,使用空行作为分隔符:

data = [] exp = [[], []] for line in myfile: if line.strip(): for index, value in enumerate(line.split()): exp[index].append(float(value)) else: data.append(exp) exp = [[], []]

并将它们全部绘制在一个图中:

for number, exp in enumerate(data, 1): plt.plot(*exp, label='experiment {}'.format(number)) plt.legend(loc='best') plt.title('My Experiments')

结果:

在此处输入图像描述

Read in the data by experiment, using the empty line as separator:

data = [] exp = [[], []] for line in myfile: if line.strip(): for index, value in enumerate(line.split()): exp[index].append(float(value)) else: data.append(exp) exp = [[], []]

and plot them all in one plot:

for number, exp in enumerate(data, 1): plt.plot(*exp, label='experiment {}'.format(number)) plt.legend(loc='best') plt.title('My Experiments')

Result:

enter image description here

更多推荐