python读文件并画图

绘制钟差残差文件脚本

时间:2020/05/15

import matplotlib.pyplot as plt
import numpy as np
import os

def plotresfile( file, outpath, intv = 60 ):
prnN = 32
lines =[]
filename = file[-13:-8]
with open(file) as fp:
lines = fp.readlines()
#
# 提取数据
lines = lines[4:]
data1 = row = []
for line in lines:
row = line.split()
data1.append(row)
#
X = row1 = []
for row1 in data1:
X.append(int(row1[1]))
#
DirtData = {}
ClkSTD = [];
for prn in range(prnN):
satData = []
for row1 in data1:
satData.append(float(row1[prn + 2]))
#
DirtData[prn] = satData
ClkSTD.append(np.std(satData)*100)
# 绘图命令
plt.figure()
ax1 = plt.subplot2grid((1, 5), (0, 0), colspan=4)
ax1.patch.set_visible(False)
prnList = []
for prn in range(prnN):
ax1.plot(X, DirtData[prn], ‘.’, markersize=2)
strprn = ‘G%02d’ % (prn + 1)
prnList.append(strprn)
#
ax1.grid(ls=’–’)
ax1.axis(‘tight’)
plt.xlabel(“week: " + filename + " time / h”, fontproperties=‘Times New Roman’, size=10)
plt.ylabel(“clock residual (m)”, fontproperties=‘Times New Roman’, size=10)
# plt.title("WEEK: " + file[:5], fontproperties=‘Times New Roman’, size=10)
plt.xlim(0, 86400 / intv)
xticks = [int(i) for i in range(0, int((86400 + 2 * 3600) / intv), int(2 * 3600 / intv))]
ylabels = range(0, 26, 2)
plt.xticks(xticks, ylabels, fontproperties=‘Times New Roman’, size=10)
plt.ylim(-0.5, 0.5)
yticks = np.arange(-0.5, 0.6, 0.1)
plt.yticks(yticks, fontproperties=‘Times New Roman’, size=10)
plt.legend(prnList, loc=1, ncol=8, fontsize=6, bbox_to_anchor=(1.01, 1.22))
plt.subplots_adjust(top=0.8)
plt.tight_layout()
#
ax2 = plt.subplot2grid((1, 5), (0, 4), colspan=1)
plt.barh(prnList,ClkSTD)
plt.xlim(0, 5)
plt.ylim(-0.2, prnN)
plt.xticks([0,2.5,5],fontproperties=‘Times New Roman’)
plt.yticks(fontproperties=‘Times New Roman’, size=9)
plt.xlabel(“std (cm)”, fontproperties=‘Times New Roman’, size=10)
plt.tight_layout()
plt.grid()
plt.savefig(outpath + ‘/’ + filename + ‘.png’)
plt.show()

print("\033[32mdrawing complete: "+ file)

end fun

if name== ‘main’:
outpath = ‘./result’
inpath = ‘./’
# 判断文件夹是否存在
isExists = os.path.exists(outpath)
if not isExists:
os.mkdir(outpath)
# 批量绘图
f_list = os.listdir(inpath)
for i in f_list:
if os.path.splitext(i)[1] == ‘.res’:
plotresfile(i, outpath)


版权声明:本文为qq_30626409原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。