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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| import os import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt
class GroupedBarChart(object): def __init__(self): self.path_current = os.path.dirname(__file__) print(self.path_current) self.path_logs = [ (self.path_current + "/" + i) for i in os.listdir(self.path_current) if (os.path.isfile(os.path.join(self.path_current, i)) and i.endswith('.txt')) ] self.num_frames_total = 0 self.num_frames_lost = 0 self.list_valid_log_name = [] self.list_frame_lost_percent = [] self.list_frame_normal_percent = [] self.font = { 'family': 'Microsoft YaHei', 'style': 'normal', 'weight': 'normal', 'size': 16 } self.title = '标题'
def Parse(self): for path_log in self.path_logs: log_name = path_log.split('/')[-1] log = open(path_log) for line in log.readlines(): if 'Flag_DataFrame' in line: self.num_frames_total += 1 elif 'Flag_FrameLost' in line: self.num_frames_lost += 1 else: pass
if self.num_frames_total is not 0: self.list_valid_log_name.append(log_name.split('_')[-1].split('.')[0]) self.frame_lost_ratio = format(self.num_frames_lost / self.num_frames_total * 100, '.2f') if self.num_frames_total is not 0 else '#NA' self.list_frame_lost_percent.append(float(self.frame_lost_ratio)) self.list_frame_normal_percent.append(100 - float(self.frame_lost_ratio))
print('\nlog file: {}'.format(log_name)) print('--------------------------------------------------------') print('num_frames_total: {}'.format(self.num_frames_total)) print(' num_frames_lost: {}'.format(self.num_frames_lost)) print('frame_lost_ratio: {}%'.format(self.frame_lost_ratio)) print('--------------------------------------------------------\n')
self.num_frames_total = 0 self.num_frames_lost = 0
def PlotSet(self): if len(self.list_valid_log_name) is not 0: ind = np.arange(len(self.list_valid_log_name)) width = 0.35 self.fig, self.ax = plt.subplots() self.rects_frame_lost = self.ax.bar( ind - width / 2, self.list_frame_lost_percent, width, color="tomato", label="label1") self.rects_frame_normal = self.ax.bar( ind + width / 2, self.list_frame_normal_percent, width, color="steelblue", label="label2") self.ax.set_ylabel('百分比 (%)', fontdict=self.font) plt.tick_params(labelsize=18) self.ax.set_title(self.title, fontdict=self.font) self.ax.set_xticks(ind) self.ax.set_xticklabels(self.list_valid_log_name, fontdict=self.font) self.ax.legend(loc='upper left', prop=self.font) print('average_frame_lost_ratio: {}%'.format(round(np.mean(self.list_frame_lost_percent), 2)))
def AutoLabel(self): """ 为每一个bar打上标注 """ for rects in (self.rects_frame_lost, self.rects_frame_normal): for rect in rects: height = rect.get_height() self.ax.text( rect.get_x() + rect.get_width() / 2., height + 1, '%.2f' % height, ha='center', va='bottom', fontsize=12)
def main(): obj = GroupedBarChart() obj.Parse() obj.PlotSet() obj.AutoLabel()
plt.show()
if __name__ == "__main__": main()
|