python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python绘制柱形图

通过python读取txt文件和绘制柱形图的实现代码

作者:Amigo瓜波

这篇文章主要介绍了通过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()

到此这篇关于通过python读取txt文件和绘制柱形图的文章就介绍到这了,更多相关python绘制柱形图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文