目的
临床数据的记录时间和对应标签(逗号后面的数字)记录在txt文件里,要把标签转换为3类标签,并且计算出每个标签的分别持续时间,然后绘制成柱形图方便查阅。
小难点分析:
(1)txt的切割读取对应内容
(2)时间差计算
txt文件如图:
使用效果
首先将原始txt转换为 左列新标签 右列持续时间
绘制为柱形图
为了直观,每次只最多显示 2个小时,同时横坐标还是按照临床的记录时间顺序。
代码实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# -*- coding: utf-8 -*-
from datetime import datetime
import matplotlib.pyplot as plt
from matplotlib.font_manager import fontproperties
font_set = fontproperties(fname = r "c:\windows\fonts\simsun.ttc" , size = 12 )
address = "d:/xxxxxx/"
patient = "xxxxx"
year = '2020' # or 2020
txt_address = address + patient + "/timetable.txt"
filename = address + patient + "/newtimetable.txt" # 保存的txt地址
# 读取txt的相应内容
def showlabel():
content = []
label = []
rawtime = []
with open (txt_address, 'r' , encoding = "utf-8" ) as f:
lines = f.readlines() # 返回的是列表
print ( "文件的行数:%d" % len (lines))
for line in lines:
content_value = line.split(sep = ',' ) # 分为[0]和[1]
time_value = content_value[ 0 ].split(sep = year)
content_value = content_value[ 1 ].split(sep = '\n' )
temp_time = year + time_value[ - 1 ]
rawtime.append(temp_time)
content.append(content_value[ 0 ])
# 去掉第一行的line和转换label
for i in range ( 1 , len (lines)): # range是左闭右开
number = int (content[i])
if number = = 4 :
label_value = 1 # label 1 表示癫痫发作
elif number = = 1 :
label_value = 0 # label 0 表示数据缺失
else :
label_value = - 1 # label -1 表示非癫痫发作
label.append(label_value)
del rawtime[ 0 ] # 去掉第一行的line
length = len (lines)
return label, rawtime, length
# 计算时间差
def gettimespan(new_label,time,length):
time_span = []
time_label = []
temp = datetime.strptime(time[ 0 ], "%y-%m-%d %h:%m:%s" )
for i in range ( 0 , length - 2 ):
if new_label[i] ! = new_label[i + 1 ]:
end_time = datetime.strptime(time[i + 1 ], "%y-%m-%d %h:%m:%s" )
diff = end_time - temp
temp = end_time
# time_span.append(diff) 标准化时间
time_span.append( round (diff.total_seconds() / 60 , 2 )) # 保留两位小数
time_label.append(new_label[i])
return time_span, time_label
# 画图,按照标签内容给图上色
def showtimeandlabel(time_span,time_label):
yanse = []
for i in time_label:
if i = = 0 :
yanse = 'blue'
elif i = = - 1 :
yanse = 'gray'
else :
yanse = 'red'
yanse.append(yanse)
plt.bar( range ( len (time_span)), time_span, width = 0.8 , tick_label = time_label, color = yanse)
plt.xlabel( '标签' , fontproperties = font_set)
plt.ylabel( "持续时间(分钟)" , fontproperties = font_set)
plt.title( "patient: " + patient, size = 20 )
plt.ylim( 0 , 120 )
plt.show()
# 保存到txt
def txtsave(time_span, time_label):
# w:向文件中写入内容时,会先清空原文件中的内容,
with open (filename, 'w' , encoding = "utf-8" ) as f:
line_number = len (time_label)
for i in range (line_number):
f.write( str (time_label[i]) + " " + str (time_span[i]))
f.write( "\n" )
def main():
new_label, time, length = showlabel()
time_span, time_label = gettimespan(new_label, time, length)
txtsave(time_span, time_label)
showtimeandlabel(time_span, time_label)
if __name__ = = '__main__' :
main()
|
到此这篇关于通过python读取txt文件和绘制柱形图的文章就介绍到这了,更多相关python绘制柱形图内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/Amigo_1997/article/details/114766182