通过python读取txt文件和绘制柱形图的实现代码
作者:Amigo瓜波 时间:2023-11-23 11:24:43
目的
临床数据的记录时间和对应标签(逗号后面的数字)记录在txt文件里,要把标签转换为3类标签,并且计算出每个标签的分别持续时间,然后绘制成柱形图方便查阅。
小难点分析:
(1)txt的切割读取对应内容
(2)时间差计算
txt文件如图:
使用效果
首先将原始txt转换为 左列新标签 右列持续时间
绘制为柱形图
为了直观,每次只最多显示 2个小时,同时横坐标还是按照临床的记录时间顺序。
代码实现
# -*- 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()
来源:https://blog.csdn.net/Amigo_1997/article/details/114766182
标签:python,柱形图,txt文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
用python自动生成日历
2022-08-28 14:28:58
![](https://img.aspxhome.com/file/2023/4/72174_0s.png)
一个ASP写的侧边菜单栏
2010-05-27 12:22:00
发现IE6下URL path不会自动补全
2009-03-31 12:52:00
Vue项目如何设置反向代理和cookie设置问题
2023-07-02 16:39:03
多次执行mysql_fetch_array()的指针归位问题探讨
2024-01-16 00:16:16
Python数据结构详细
2022-10-03 21:02:00
一篇文章带你学习Python3的高阶函数
2021-04-24 00:58:51
shell命令行,一键创建 python 模板文件脚本方法
2023-08-03 18:33:26
python numpy 一维数组转变为多维数组的实例
2023-03-19 23:30:10
![](https://img.aspxhome.com/file/2023/0/90370_0s.jpg)
asp下查询xml的实现代码
2011-04-19 10:37:00
OpenSearch 初探
2008-06-19 12:06:00
![](https://img.aspxhome.com/file/UploadPic/20086/19/2008619121131785s.png)
怎样修改 MySQL数据库中的密码
2008-11-27 15:35:00
pandas去除重复列的实现方法
2022-06-27 12:37:31
![](https://img.aspxhome.com/file/2023/9/91859_0s.png)
Python中aiohttp的简单使用
2022-05-29 05:43:33
Python 格式化打印json数据方法(展开状态)
2023-10-07 05:29:50
MYSQL教程:服务器优化和硬件优化
2009-02-27 15:43:00
mybatis-plus的sql语句打印问题小结
2024-01-25 11:23:42
python3模拟实现xshell远程执行linux命令的方法
2022-05-26 11:45:14
![](https://img.aspxhome.com/file/2023/0/118390_0s.jpg)
python代码 FTP备份交换机配置脚本实例解析
2023-05-08 22:47:25
![](https://img.aspxhome.com/file/2023/7/70807_0s.png)
Python3实现的Mysql数据库操作封装类
2024-01-20 23:57:36