读取txt文件,将匹配条件的多列绘制成曲线展示:
import matplotlib.pyplot as plt
import re
from datetime import datetime
from pylab import mpl
mpl.rcParams["font.sans-serif"] = ["SimHei"] # 设置显示中文字体
mpl.rcParams["axes.unicode_minus"] = False # 设置正常显示符号
"""
out_los_GDHZ.txt文件内容
2024/03/05 05:50:01 sys: 1,prn: 8, f : 0, rr : 22776258.520521, cdts : 9499.326017, dtrpr : 0.000000, ionxr : 16.994414, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 8, f : 0, rr : 22776258.520521, cdts : 9499.326017, dtrpr : 0.000000, ionxr : 16.994414, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 8, f : 1, rr : 22776258.520521, cdts : 9499.326017, dtrpr : 0.000000, ionxr : 30.475578, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 8, f : 1, rr : 22776258.520521, cdts : 9499.326017, dtrpr : 0.000000, ionxr : 30.475578, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 14, f : 0, rr : 23343648.060891, cdts : 102648.741372, dtrpr : 0.000000, ionxr : 19.831918, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 14, f : 0, rr : 23343648.060891, cdts : 102648.741372, dtrpr : 0.000000, ionxr : 19.831918, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 14, f : 1, rr : 23343648.060891, cdts : 102648.741372, dtrpr : 0.000000, ionxr : 35.563990, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 14, f : 1, rr : 23343648.060891, cdts : 102648.741372, dtrpr : 0.000000, ionxr : 35.563990, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 30, f : 0, rr : 21635324.825727, cdts : -127128.592610, dtrpr : 0.000000, ionxr : 14.159144, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
2024/03/05 05:50:01 sys: 1,prn: 30, f : 0, rr : 21635324.825727, cdts : -127128.592610, dtrpr : 0.000000, ionxr : 14.159144, dantr : 0.000000, dantrr[findex] : 0.000000, scb : 0.000000,
"""
with open('out_los_GDHZ.txt', 'r') as file:
timeList1 = []
ionxrList1 = []
timeList2 = []
ionxrList2 = []
timeList3 = []
ionxrList3 = []
timeList4 = []
ionxrList4 = []
count1, count2, count3, count4 = 0, 0, 0, 0
lines = file.readlines()
# 因有数据重复,隔行读取数据
for line in lines[::2]:
# 多个分隔符来分割
temp = re.split("[,| ]+", line)
if (temp[3] == "1") & (temp[5] == "8") & (temp[8] == "0"):
datee = temp[0]
timee = temp[1]
ionxr = temp[20]
dt = datee + " " + timee
timeArry = datetime.strptime(dt, "%Y/%m/%d %H:%M:%S")
timeList1.append(timeArry)
ionxrList1.append(ionxr)
count1 += 1
# print(timeArry,ionxr)
elif (temp[3] == "8") & (temp[5] == "33") & (temp[8] == "1"):
datee = temp[0]
timee = temp[1]
ionxr = temp[20]
dt = datee + " " + timee
timeArry = datetime.strptime(dt, "%Y/%m/%d %H:%M:%S")
timeList2.append(timeArry)
ionxrList2.append(ionxr)
count2 += 1
# print(timeArry,ionxr)
elif (temp[3] == "32") & (temp[5] == "24") & (temp[8] == "0"):
datee = temp[0]
timee = temp[1]
ionxr = temp[20]
dt = datee + " " + timee
timeArry = datetime.strptime(dt, "%Y/%m/%d %H:%M:%S")
timeList3.append(timeArry)
ionxrList3.append(ionxr)
count3 += 1
# print(timeArry,ionxr)
elif (temp[3] == "32") & (temp[5] == "40") & (temp[8] == "1"):
datee = temp[0]
timee = temp[1]
ionxr = temp[20]
dt = datee + " " + timee
timeArry = datetime.strptime(dt, "%Y/%m/%d %H:%M:%S")
timeList4.append(timeArry)
ionxrList4.append(ionxr)
count4 += 1
# print(timeArry,ionxr)
# 绘制线形图
plt.title(u'电离层活跃状况')
plt.plot(timeList1, ionxrList1, color='green', label='1-8-0')
plt.plot(timeList2, ionxrList2, color='red', label='8-33-1')
plt.plot(timeList3, ionxrList3, color='blue', label='32-24-0')
plt.plot(timeList4, ionxrList4, color='cyan', label='32-40-1')
print(count1, count2, count3, count4)
# 设置x轴标签和y轴标签
plt.xlabel(u"日期")
plt.ylabel(u"电离层系数")
# 显示图表
plt.show()
效果如下:因百万条数据量,y轴数字叠加显示。