编程语言
首页 > 编程语言> > 通过python读取txt文件和绘制柱形图

通过python读取txt文件和绘制柱形图

作者:互联网

目录

目的

使用效果

代码实现


目的

临床数据的记录时间对应标签(逗号后面的数字)记录在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()

 

标签:span,python,柱形图,label,content,value,time,txt
来源: https://blog.csdn.net/Amigo_1997/article/details/114766182