我有如下所示的输入数据,我要平均第6列和该列的标准差。我还需要一个图形,其中第一列将在x轴上,平均值将在y轴上,并带有误差线。
我附上了只能绘制第一列与第六列的脚本。我没有太多的想法做平均值和一起绘制。任何帮助都会对我有帮助。
脚本:
import numpy as np
import matplotlib.pyplot as plt
x1,x2,y1, y2 = [], [], [],[]
label_added =False
with open("1.txt") as f:
for line in f:
cols = line.split()
x1.append(float(cols[0]))
y1.append(float(cols[5]))
if not label_added:
plt.plot(x1,y1,'r-', label="300_temp")
label_added = True
else:
plt.plot(x1,y1,'r-')
label_added =False
with open("2.txt") as f:
for line in f:
cols = line.split()
x2.append(float(cols[0]))
y2.append(float(cols[5]))
if not label_added:
plt.plot(x2,y2, 'g-', label="800_temp")
label_added = True
else:
plt.plot(x2,y2, 'g-')
plt.title('final_output')
plt.xlabel('time_fs')
plt.ylabel('intersitial')
plt.legend()
plt.tight_layout()
plt.savefig("final_interstitial.jpeg", dpi=100)
输入数据结构:
1.txt
40.1 -970181.423308824 25086.8510704775 1030.68868052956 2.98863069261149 34845
40.2 -969291.275241766 24578.0340950803 1002.86354474784 3.27434173388944 40208.5
40.3 -968489.350679405 24160.1307947391 977.795055894274 3.55155208480988 45345
40.4 -967676.040718834 23644.7886925808 952.370742000842 3.81838293934396 50205
40.5 -966981.971290069 23225.0631104031 930.672470146222 4.07354498687891 55854.5
40.6 -966254.82735723 22651.1303668863 907.940243789837 4.31555138493202 62278.5
40.7 -965668.239087129 22190.7422743739 889.603544698553 4.54318654063522 67333
.
.
.
2.txt
40.1 -955398.198359867 33344.4512324167 1408.73933784128 3.12396891367147 36796.5
40.2 -954229.783369542 32683.9304617525 1372.22031719846 3.42945308560201 42943.5
40.3 -953191.590417265 32095.1208511191 1339.76973308475 3.73344595502824 49085
40.4 -952117.587463572 31487.7205358262 1306.19919339234 4.03307586152993 56499.5
40.5 -951132.223115772 30875.4404971051 1275.39557738745 4.32525826680283 64040.5
40.6 -950246.534420928 30277.6289073256 1247.7121918422 4.60798342893888 71283
40.7 -949410.920964954 29712.2289807824 1221.59019340933 4.8790799203458 78363.5
.
.
.
答案 0 :(得分:0)
如果我对您的理解正确,则希望在第一个文件中绘制观测值,在第二个文件中绘制相应的观测值,然后对所有观测值绘制这两个值之间的平均值。做到这一点的一个好方法是首先定义一个函数,该函数读取任何文件,然后使用numpy将数据格式化为数值:
import numpy as np
import matplotlib.pyplot as plt
def read_data(filename):
with open(filename) as f:
content = f.read().splitlines()
content = [[float(col) for col in row.split(' ')] for row in content]
return np.array(content)
然后,我们读取文件并以平均值绘制数据。
data_1 = read_data('1.txt')
data_2 = read_data('2.txt')
plt.plot(data_1[:, 0], data_1[:, 5], 'g-', label="300_temp")
plt.plot(data_2[:, 0], data_2[:, 5], 'g-', label="800_temp")
# Average of column 5 from both files
plt.plot(data_2[:, 0], (data_1[:, 5] + data_2[:, 5]) / 2, 'g-', label="300/800
temp avg")
plt.show()
在这里,我们利用numpy的工作原理。 numpy数组是二维的,data[:, n]
只是意味着我们提取所有行,第n
列被提取,反之亦然。