#-*- coding:utf-8 -*-
import os
import numpy as np
import random
import matplotlib.pyplot as plt
'''
log里的数据格式:
1次测量数据: IC1: ce111 cell2 …… cell36
IC2: ce111 cell2 …… cell36
……
IC7: ce111 cell2 …… cell36
2次测量数据: IC1: ce111 cell2 …… cell36
IC2: ce111 cell2 …… cell36
……
IC7: ce111 cell2 …… cell36
……
n次测量数据: IC1: ce111 cell2 …… cell36
IC2: ce111 cell2 …… cell36
……
IC7: ce111 cell2 …… cell36
'''
ic = 7 #总的IC数
cell = 36 #每个IC里的cell数
ad_count = 100 #AD采样次数
num_list = [] #所有的测量数据
plot_line = 36 #plot_line个cell画在一张图中,总共图片数为:ic*(cell/plot_line)
def save_log(pwd):
print("save_log")
my_log = pwd + '\\my_test.log'
flog = open(my_log,'w+')
for i in range(ic*ad_count):
my_list = [random.random() for cell_i in range(cell)] #随机生成cell个随机数
my_str = ''
for float_num in my_list:
my_str = my_str + str(round(float_num,2)) + ' '
flog.write(my_str + '\n') #将cell个随机数以字符串形式保存,模拟生成log
flog.close()
def get_log(pwd):
print("get_log")
my_log = pwd + '\\my_test.log'
flog = open(my_log,'r')
for line in flog:
#print(line)
my_str_list = line.split()
my_list = [float(my_str) for my_str in my_str_list] #字符串读进来转成float
for i in range(cell-len(my_list)):
my_list.append(0) #补0,防止维度不统一,生成二维数组时出错(最后停止打印时cell没有全部打完)
#print(my_list)
num_list.append(my_list)
flog.close()
def plot_log():
print("plot_log")
num_array = np.array(num_list)
for ic_i in range(ic):
for cell_i in range(cell):
plot_array = num_array[ic_i: :ic, cell_i] #每行对应一个IC的n个cell,取数是从下标ic_i开始到结束,间隔为ic
#print(plot_array)
plt.figure(ic_i*(cell/plot_line)+cell_i/plot_line+1) #每plot_line个画在一起
plt.subplot(plot_line,1,cell_i%plot_line+1)
plt.plot(plot_array, color='red', linewidth=0.5, linestyle='-')
plt.xticks([]) #去掉x轴刻度
plt.yticks([]) #去掉y轴刻度
plt.ylabel(str(cell_i+1), fontsize=5, rotation='horizontal')
#plt.plot(plot_array, label = 'cell'+str(cell_i+1))
#plt.legend(loc = 0) #图例位置自动
#plt.grid(True) #打开坐标轴小网格
plt.title('IC' + str(ic_i+1) + ' analysis', y=-3) #标题置于最下方
plt.show()
if __name__ == '__main__':
pwd = os.getcwd()
save_log(pwd) #模拟生成log
get_log(pwd) #将log处理成二维数组
#print(num_list)
plot_log() #画图
python3处理log数据并plot
最新推荐文章于 2025-05-09 17:07:37 发布